Page 1 of 1

[solved] find within a string

Posted: Thu May 05, 2005 4:12 am
by phpScott
I suck at writing regual expressions so if some one call help that would great.

String looks like.

background: rgb(156, 123, 173)
url(/images/interface/side_bar_02.gif) repeat-x scroll 0%; font-family:
verdana,arial,sans-serif; color: rgb(156, 123, 173); line-height:
1.5em; -moz-background-clip: initial; -moz-background-origin: initial;
-moz-background-inline-policy: initial; font-size: 0.8em;

I need to find font-size: 0.8em;

so I would like to do a search for 'font-size:' and the 'em;' part regardless what the number in the middle is set too.

this is client side as it is manipulting the css stylesheet.
Cheers

phpScott

Posted: Thu May 05, 2005 6:42 am
by infolock
you could do something like this :

Code: Select all

$str = explode(',','
background: rgb(156, 123, 173) 
url(/images/interface/side_bar_02.gif) repeat-x scroll 0%; font-family: 
verdana,arial,sans-serif; color: rgb(156, 123, 173); line-height: 
1.5em; -moz-background-clip: initial; -moz-background-origin: initial; 
-moz-background-inline-policy: initial; font-size: 0.8em;');

for($i=0; $i<count($str); $i++)
{
   if($i==7)
   {
      echo $str[$i];
   }
}
of course this assumes that your font will be the 7th semi-colon delimited entry in the string every time. you coudl also do a array search to find the font-size pattern, but this should be enough to get you started ;)

hope this helps...

Posted: Thu May 05, 2005 7:55 am
by phpScott
solved while waiting for replies?

Code: Select all

find = &quote;font-size: &quote; + currentSize + &quote;em;&quote;;
newSize = &quote;font-size: &quote; + size + &quote;em;&quote;;
testing = bodyRule.replace(find, newSize);
I know that replace should be using a regular expression but it seems to work.

Cheers though.

phpScott

Posted: Thu May 05, 2005 12:19 pm
by Chris Corbyn
phpScott wrote: I know that replace should be using a regular expression but it seems to work.
The replace() method CAN take a regular expression as an argument but it will work with a string since /hello world/ is still a regular expression, just no meta characters in there ;-)