REGEX replace line for forum threads
I am trying to replace text from an outdated mod on a forum. The old code looked like this
[ame="http://www.youtube.com/watch?v=wYJ20INbM7Q"]YouTube - ‪Bill O'Reilly Interviews Rapper Lupe Fiasco - 06/20/11‬‏[/ame]
I want the new code to look like this:
[video=youtube;wYJ20INbM7Q]http://www.youtube.com/watch?v=wYJ20INbM7Q[/video]YouTube - Bill O'Reilly Interviews Rapper Lupe 开发者_运维技巧Fiasco - 06/20/11[/video]
I used:
$text = preg_replace('[ame="http://www.youtube.com/watch?v="([a-z0-9]+)\"],
([video=youtube;$2]http://www.youtube.com/watch?v=$2[/video])', $text);
Error: Warning: Wrong parameter count for preg_replace()
Any help appreciated.
Also, your code won't do what you want it to. Use these instead.
$regEx = '#\[ame\=".*?\=([a-zA-Z0-9]*?)"]#';
$replacement = "([video=youtube;$1]http://www.youtube.com/watch?v=$1[/video])";
$text = preg_replace($regEx, $replacement, $text);
You are missing quotes before and after the first comma in your parameter list
$text = preg_replace('|\[ame="http://www.youtube.com/watch\?v=([a-z0-9]+)"\]|i', '[video=youtube;$1]http://www.youtube.com/watch?v=$1[/video]', $text);
Also, your regex has a number of syntax errors:
- You need to identify the start and end of your pattern (I used
|
above) - You need to escape your square brackets
- You don't need to escape your quote, and you have an extra quote after v=
- You need an
i
modifier to make your match case-insensitive
精彩评论