2012-04-02 28 views
-1

i need to do thispreg_replace在加入時不工作<a href

replacing <a href="forum.php?id=1">title</a> with <a href="f-1-title.html">title</a>

i do this patren

$prased_template=preg_replace('#<a href="forum.php?id=(.*?)">(.+?)</a>@','test=\\3',$template); 

but its not work , i need to get the ID and Title to put it in second value

how !

回答

0
$replaced = preg_replace('#<a href="forum.php\?id=(\d+)">([^<]+)</a>#i','<a href="f-$1-$2.html">$2</a>', $template); 
+0

not working its return with old href – 2012-04-02 22:43:12

0

Your regex has a couple of issues. First, the delimiters don't match. You open with # but close with @; this won't work. Stick with one. You also aren't escaping other characters in the string. Use this and it should work:

#<a href="forum\.php\?id=(.*?)">(.+?)</a># 
+0

its not working its return with same old href – 2012-04-02 22:43:01

+0

thank you , but your regex work only when i remove ( 2012-04-02 22:48:55

+0

Works for me.. '$template = 'title';''$ prased_template = preg_replace('#(.+?)#','test = \\ 3',$ template);''echo $ prased_template;''test =' – spencercw 2012-04-02 22:54:49