PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Nov 18, 2019 7:43 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: preg_replace
PostPosted: Thu May 12, 2005 5:17 am 
Offline
Forum Contributor
User avatar

Joined: Thu May 12, 2005 5:06 am
Posts: 153
Location: UK


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 12, 2005 6:34 am 
Offline
Forum Contributor

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

Syntax: [ Download ] [ Hide ]
$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...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 12, 2005 6:44 am 
Offline
Forum Contributor
User avatar

Joined: Thu May 12, 2005 5:06 am
Posts: 153
Location: UK
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 12, 2005 6:48 am 
Offline
Forum Contributor

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 1:52 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 3:03 am 
Offline
DevNet Resident

Joined: Tue Nov 18, 2003 2:09 pm
Posts: 1826
Location: Middlesbrough, UK


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 3:26 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 6:23 pm 
Offline
DevNet Resident

Joined: Tue Nov 18, 2003 2:09 pm
Posts: 1826
Location: Middlesbrough, UK
and
<!-- ************** address search form ************** -->
content.....
<!-- ************** /address search form ************** -->

?>


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 6:36 pm 
Offline
Spockulator
User avatar

Joined: Wed Feb 04, 2004 9:15 pm
Posts: 4714
Location: Eden, Utah


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 6:39 pm 
Offline
DevNet Resident

Joined: Tue Nov 18, 2003 2:09 pm
Posts: 1826
Location: Middlesbrough, UK


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 13, 2005 6:46 pm 
Offline
Spockulator
User avatar

Joined: Wed Feb 04, 2004 9:15 pm
Posts: 4714
Location: Eden, Utah
ahh yes, you need to escape the * with \

like this:

Syntax: [ Download ] [ Hide ]
<?

$var = <<<STOP

<!-- ************** address search form ************** -->

content.....

<!-- ************** /address search form ************** -->

STOP
;

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

?>


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 14, 2005 11:40 am 
Offline
Breakbeat Nuttzer
User avatar

Joined: Wed Mar 24, 2004 8:57 am
Posts: 13098
Location: Melbourne, Australia
I see what you want mal... so you can get anything that's in between the comment tags right?

Syntax: [ Download ] [ Hide ]
$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).


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 17, 2005 12:26 am 
Offline
Spockulator
User avatar

Joined: Wed Feb 04, 2004 9:15 pm
Posts: 4714
Location: Eden, Utah


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group