...

View Full Version : preg_match detection w/ new element



sitNsmile
11-18-2010, 10:08 AM
if I have


$data = '[IMG,height=200,width=200]';

preg_match('/IMG/'); // this will find the IMG in the string,



but the question is, how would I detect what height is equal too, even if I put /height/ . ?

I want to it so I can grab the variables in a string.

Thanks

ssonawa
11-18-2010, 10:35 AM
Hi,

why don't you use explode to separate this string by comma.

your second element in the resulting array will be height=200. You can easily get the value of height from it, using the same logic.

hope it helps. try out.

sitNsmile
11-18-2010, 10:39 AM
Hi,

why don't you use explode to separate this string by comma.

your second element in the resulting array will be height=200. You can easily get the value of height from it, using the same logic.

hope it helps. try out.


Well something like




$data = '[IMG,height=200,width=200]';

explode(',',$data); // will break it down.



I wasn't sure if there was some internal php function that could give me the same results more efficiently

poyzn
11-18-2010, 10:41 AM
try this


$string = '[IMG,height=200,width=200]';
preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/', $string, $matches);
print_r($matches)

you must receive next result
$match[1] = IMG value
$match[2] = height value
$match[3] = width value

sitNsmile
11-18-2010, 10:47 AM
try this


$string = '[IMG,height=200,width=200]';
preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/', $string, $matches);
print_r($matches)

you must receive next result
$match[1] = IMG value
$match[2] = height value
$match[3] = width value

Thats something I was looking into,
question about the markings within preg_match or even something you would use in .htaccess, where do you learn those meanings ? like [0-9]..etc

poyzn
11-18-2010, 10:53 AM
this is a regular expression. just google it or you can find more info here http://en.wikipedia.org/wiki/Regular_expression

sitNsmile
11-18-2010, 11:18 AM
this is a regular expression. just google it or you can find more info here http://en.wikipedia.org/wiki/Regular_expression

Okay to continue my quest of this, what would be a good regex for preg_replace after I have my data.

this needs to just replace the IMG code with $newString,
making this abstract for me to use this on other elements and no conflicts will cross. Thanks



$string = '<table>[IMG,height=200,width=200]</table>';
preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/', $string, $matches);
$newString = "<img src='' height=$matches[3] width= $matches[2] />"
preg_replace("/[IMG([\w]+?)]/",$newString,$string);

poyzn
11-18-2010, 11:26 AM
I didn.t get what you want to do, but you can try this code :)



$string = '[IMG,height=200,width=200]';
preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/', $string, $matches);

$string = '<table>[IMG,height=200,width=200]</table>';
$newString = "<img src='' height=$matches[3] width=$matches[2] />";

echo preg_replace("/\[IMG[^\]]+\]/",$newString,$string);

sitNsmile
11-18-2010, 11:39 AM
I didn.t get what you want to do, but you can try this code :)



$string = '[IMG,height=200,width=200]';
preg_match('/\[(IMG)\,height\=([0-9]+)\,width\=([0-9]+)\]/', $string, $matches);

$string = '<table>[IMG,height=200,width=200]</table>';
$newString = "<img src='' height=$matches[3] width=$matches[2] />";

echo preg_replace("/\[IMG[^\]]+\]/",$newString,$string);


That works great. thank you!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum