Forum Moderators: coopster
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);
Thanks for read me.
$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(¦)!
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?
That:
$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
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!
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]
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."'");
$estado=mysql_fetch_array($consultaTipo);
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>';
}