Page 1 of 1

preg_replace

Posted: Thu May 12, 2005 5:17 am
by ed209
Hi
I'm not trained as a programmer but I get by in PHP, well I thought I did until I found this function! Basically, I have a problem where javascript is removing " " from my class atribute in html so

Code: Select all

<span class=&quote;a-class&quote;>some text</span>
comes out like

Code: Select all

<span class=a-class>some text</span>
What I would like to be able to do is put back the " ". Here is what I have so far, I have only just looked at regular expression so go easy on me!

Code: Select all

$str = &quote; Here is some sample html that is <span class=aClass> getting broken </span>&quote;;
	echo preg_replace(  '/class=ї^&quote;]+>/', 'class=&quote;\\0&quote; >', $str);
	
	/*
	 ////currently outputs 
	 Here is some sample html that is <span class=&quote;class=aClass> getting broken </span>&quote; >
	
	 ////SHOULD output 
	 Here is some smaple html that is <span class=&quote;aClass&quote;> getting broken </span>
	*/
thanks.

Posted: Thu May 12, 2005 6:34 am
by Bennettman
Use 1 as a callback instead of 0 and add parentheses. 0 returns the whole matched string, 1 onwards returns parenthesis matches. Here's what I think would work:

Code: Select all

$str = " Here is some sample html that is <span class=aClass> getting broken </span>";
echo preg_replace(  '/class=([-_a-z0-9]*?)/i', 'class="\\1"', $str);
Not tested (at college, sucks) and I'm not a master of regexp, but hey.

However, if I'm thinking right, the HTML should still work without quotes as long as there's no space in the name...

Posted: Thu May 12, 2005 6:44 am
by ed209
thanks Bennettman, that works perfectly! I don't understand the callback thing yet, looks like I'll have to read about that one. You're right in saying that the class would still work but it wouldn't validate as xhtml without the quotes.
thanks,
ed.

Posted: Thu May 12, 2005 6:48 am
by Bennettman
Callback is really simple - it just returns matches and parts of them:

If you have say /class="(*?)"/i (which matches anything starting with class" and ending with ", case-insensitive), \\0 (or $0) will return everything (as an example, it might return class="moo"), and \\1 or $1 will return the contents of the first set of parentheses (in the example, it'll return moo). If you have a second set of parentheses in the search, \\2 or $2 will use that.

But, I wouldn't really worry about getting stuck there. Regexp is a real pain at times.

Posted: Fri May 13, 2005 1:52 am
by Chris Corbyn
Moved to regex

Posted: Fri May 13, 2005 3:03 am
by malcolmboston
ive got a question

how would the regex string look like if i wanted toget everything in between...

Code: Select all

<!-- results -->
RESULTS HERE, THIS IS WHAT I WANT
<!-- /results -->

Posted: Fri May 13, 2005 3:26 am
by Chris Corbyn
malcolmboston wrote:ive got a question

how would the regex string look like if i wanted toget everything in between...

Code: Select all

&lt;!-- results --&gt;
RESULTS HERE, THIS IS WHAT I WANT
&lt;!-- /results --&gt;

Code: Select all

$string = <<<STOP
<!-- results -->
RESULTS HERE, THIS IS WHAT I WANT
<!-- /results -->
STOP;

preg_match('#<!-- results -->(.*?)<!-- /results -->#s', $string, $matches);

echo $matches[1];

Posted: Fri May 13, 2005 6:23 pm
by malcolmboston
and
<!-- ************** address search form ************** -->
content.....
<!-- ************** /address search form ************** -->

?>

Posted: Fri May 13, 2005 6:36 pm
by Burrito
would be the same as D11 just posted only replace the "results" with "***** address search form ****"etc.

the non-greedy "." should get it all

*I think* 8O

Posted: Fri May 13, 2005 6:39 pm
by malcolmboston
well it doesnt work :/

Posted: Fri May 13, 2005 6:46 pm
by Burrito
ahh yes, you need to escape the * with \

like this:

Code: Select all

<?
$var = <<<STOP
<!-- ************** address search form ************** --> 
content..... 
<!-- ************** /address search form ************** --> 
STOP;
preg_match('#<!-- \*\*\*\*\*\*\*\*\*\*\*\*\*\* address search form \*\*\*\*\*\*\*\*\*\*\*\*\*\* -->(.*?)<!-- \*\*\*\*\*\*\*\*\*\*\*\*\*\* /address search form \*\*\*\*\*\*\*\*\*\*\*\*\*\* -->#s', $var, $matches); echo $matches[1];
?>

Posted: Sat May 14, 2005 11:40 am
by Chris Corbyn
I see what you want mal... so you can get anything that's in between the comment tags right?

Code: Select all

$string = <<<STOP
<!-- results -->
RESULTS HERE, THIS IS WHAT I WANT
<!-- /results -->
STOP;

//Can genralised as....

preg_match('#<!--.*?-->(.*?)<!--\s*/.*?-->#s', $string, $matches);

echo $matches[1];
You could feed the key name into a function too (But you'll need escape all of the metacharcters - I wish there was a function for this).

Posted: Tue May 17, 2005 12:26 am
by Burrito
d11wtq wrote: ...(But you'll need escape all of the metacharcters - I wish there was a function for this).
so write one for us d11! You got nothing better to do...the new job doesn't start for another month 8O