Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

A function in preg replace, works?



5:45 pm on Sep 26, 2006 (gmt 0)

5+ Year Member


I'll use a function (crearvinculo()) in a preg_replace, but the functon doesn't get the variable.

Here's the code:

$txt = preg_replace("/\[enlaceGrupo\¦(.*?)\]/", crearvinculo('\\1'),$txt);

I've tried to use the "e" and \\1, like the manual in php.net, and \1, $1.. Nothing works, and the variant with de "e" and \\1 give's me an php error.

Thanks for read me.


6:54 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member

Do you mean for it to be something like this?:

$txt = preg_replace("/\[enlaceGrupo\¦(.*?)\]/", crearvinculo("$1"),$txt);

It's hard to understand what you are asking. If this doesn't work, would you mind clarifying?

Good luck!

P.S. Make sure to replace the pipe character(¦)!


10:04 pm on Sep 26, 2006 (gmt 0)

5+ Year Member

Thanks for answer eelixduppy.

I'm sorry for my english :P. I try to explain me better, but i think you've understand me.

I've a function, that change some "codes" from a text. The text come from a database. I'll change something like bbcode (bold, italic, links, and some others). Thats works whitout any problems.
One of the things to change, is a "type of url".

The site tell about rocks bands, from the Canary Islands. Bands who's playin' today, and disappeared bands. The url of the bands are mysite.net/bands/active/Band-Name, or mysite.net/bands/disappeared/Band-Name.
To make it eassier for people to refresh the información of the site (we are four persons), and don't need html, in the biography of any band, sometimes talk about other band, and to make a link to their biography, i will make it with the preg_replace. But, i must now, is the band active, or not, to make the link.

Then... I use the function, in preg_replace, and preg_replace is in another function. I now i can make two preg_replace's, one for active bands (the i put something like [activeBand¦BandName] and another for disappeared bands... But, I'll know, it's posible with this way?

$txt = preg_replace("/\[enlaceGrupo\¦(.*?)\]/", crearvinculo("$1"),$txt);
Don't work :P. I've to prove with "\1" and "\\1", but not work. (i also have change the pipe simbol (¦).

I hope you can understand me.

Thanks. And sorry for the long explainin' :P


10:21 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 5+ Year Member

>>>it's posible with this way?

Yes, it is possible to put a function call within the replacement argument in preg_replace. crearvinculo("$1") should work properly.

What may not be working properly is your pattern. There may also be some errors in the page. Keep crearvinculo("$1") and check to make sure that your pattern is correct, and that there are no errors in the script (for this, add error_reporting [us3.php.net](E_ALL); ) to the top of the script ).

Best of luck!


3:07 am on Sep 27, 2006 (gmt 0)

5+ Year Member

Hi pzin!

I understand what are you trying to do, because I manage the same kind of website (in spanish too).

I tested your code and works fine. Try with this:

$txt = 'aaaa bbbb cccc [enlaceGrupo¦Grupo1] ddd eee [enlaceGrupo¦Grupo2].';

$txt = preg_replace('#\[enlaceGrupo\¦(.*?)\]#is', crearvinculo("\\1"), $txt);

echo $txt;

function crearvinculo($str)
return '<a href="#">' . $str . '</a>';

Note: Replace the ¦ bar with solid.

I created the same idea for my website but the diference is if a user type a band name that we have in database, the link to that band is created without special codes (bbcode).

[edited by: Psychopsia at 3:07 am (utc) on Sep. 27, 2006]


7:06 pm on Sep 27, 2006 (gmt 0)

5+ Year Member

Hi. Thanks for answer again.

Now, I've probe the code from Psychopsia, and don't work.

I post the crearvinculo(), because i think the problem is there.

function crearvinculo($clave) {
$consultaTipo=mysql_query("select * from lrGrupos where clave='".$clave."'");
if($estado['tipo']==0) return '<a href="/grupos/desaparecidos/'.$estado['clave'].'" title="'.$estado['nombre'].'">'.$estado['nombre'].'</a>';
if($estado['tipo']==1) return '<a href="/grupos/en-activo/'.$estado['clave'].'" title="'.$estado['nombre'].'">'.$estado['nombre'].'</a>';


8:38 pm on Oct 3, 2006 (gmt 0)

5+ Year Member

Hi again. I try to bring it to work, but after one week, i give up :(


8:57 pm on Oct 3, 2006 (gmt 0)

5+ Year Member


What PHP version are you using?

I'm try this code with 4.3 and 5, and it works.


9:52 pm on Oct 3, 2006 (gmt 0)

5+ Year Member


I am using version 4.3.8. The"re give me no errors. I don"t understand it :/


Featured Threads

Hot Threads This Week

Hot Threads This Month