View Full Version : Need a quick help regarding regular expression (P
Jaffery
12-22-2009, 06:08 PM
I need little help of any php programmer here for preg_replace.
What will be the regex to match following :
[img width=320 height=217]
Here value for width and height changes.
Actually I need it for cleaner.php , asking here as on vbulletin.com, there is not much help in this matter.
The Geek
12-22-2009, 07:06 PM
Not 100% sure of what you're trying to do but this would grab the values from the above string:
\[img width=(\d*) height=(\d*)\]
so this:
$subject = '[img width=320 height=217]';
$find = '/\[img width=(\d*) height=(\d*)\]/im';
$replace = 'width=\1 and height=\2';
$result = preg_replace($find, $replace , $subject);
would give you:
width=320 and height=217
Jaffery
12-23-2009, 05:28 AM
Using cleaner.php, I need to change all instances of [img width=320 height=217] to [img] in my forum which I recently converted from smf to vb.
I will try it and let you know.
btw, just asking as I am still understanding regex, what does widht=\1 and height width=\2 stands for. ie. \1 and \2 , what they denote ?
Thanx :)
Update: Thanx buddy, it helped.
The Geek
12-23-2009, 07:35 AM
Using cleaner.php, I need to change all instances of [img width=320 height=217] to [img] in my forum which I recently converted from smf to vb.
I will try it and let you know.
btw, just asking as I am still understanding regex, what does widht=\1 and height width=\2 stands for. ie. \1 and \2 , what they denote ?
Thanx :)
Update: Thanx buddy, it helped.
in the regex I gave you, the first (\d*) goes into \1 and the second one goes into \2
Jaffery
12-28-2010, 05:06 AM
I need little more help..
What would be regex to match following :
rather I need to convert :
[youtube=425,350]NN7XXCDqzVg
TO
http://www.youtube.com/watch?v=NN7XXCDqzVg
ForumsMods
12-28-2010, 06:24 AM
Use the following regular expressions:
/\[youtube=(.*)\](.*)\[\/youtube\]/im
Jaffery
12-28-2010, 07:54 AM
That would match whole following..
NN7XXCDqzVg[/video]
However, I need to grab value which is in bold and then
Put it in :
[code]http://www.youtube.com/watch?v=NN7XXCDqzVg
I can do following:
$subject = '[youtube=425,350]NN7XXCDqzVg';
$find = '/\[a-zA-Z-]*\/im';
$replace = 'http://www.youtube.com/watch?v=\3';
$result = preg_replace($find, $replace , $subject);
Now kindly advice if that is ok as I am still not confident in regex and had hardly used backreference before.
ForumsMods
12-28-2010, 02:21 PM
Use this:
$subject = 'NN7XXCDqzVg';
$find = '/\[youtube=(.*)\](.*)\[\/youtube\]/im';
$replace = 'http://www.youtube.com/watch?v=\2';
$result = preg_replace($find, $replace , $subject);
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.