preg_replace

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

preg_replace

Post 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.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post 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...
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post 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.
Bennettman
Forum Contributor
Posts: 130
Joined: Sat Jun 15, 2002 3:58 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Moved to regex
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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 -->
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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];
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

and
<!-- ************** address search form ************** -->
content.....
<!-- ************** /address search form ************** -->

?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

well it doesnt work :/
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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];
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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
Post Reply