Reply
help with preg_replace
Old 04-29-2007, 07:57 AM help with preg_replace
Average Talker

Posts: 29
Hi, I'm having a little trouble with the preg_replace functon

Basically, i don't know where to start!

I want to replace something like this:
Code:
<script type="text/javascript" src="Y"></script>
with this
Code:
<script type="text/javascript" src="XY"></script>
Notice the added X before the Y link in src"".

Thanks in advance
__________________
-ValC
ValC is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 04-29-2007, 01:14 PM Re: help with preg_replace
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
You don't need the whole preg machinery to do this.

If you know exactly what must be changed to what, use str_replace
http://www.php.net/manual/en/function.str-replace.php
PHP Code:
$html='<script type="text/javascript" src="Y"></script>';
$html=str_replace('src="Y"''src="XY"'$html); 
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 04-29-2007, 02:10 PM Re: help with preg_replace
kaisellgren's Avatar
Extreme Talker

Posts: 214
Name: Kai Sellgren
Location: Finland
Like tripy said, do it.

It can be done with preg_replace() this way:
PHP Code:
<?php
$string 
'<script type="text/javascript" src="Y"></script>';
$string preg_replace('/(.*?src=")(.*?)(".*)/','$1X$2$3',$string);
echo 
$string;
?>
__________________
Adept Web Community
kaisellgren is offline
Reply With Quote
View Public Profile Visit kaisellgren's homepage!
 
Old 04-29-2007, 02:35 PM Re: help with preg_replace
solomongaby's Avatar
Webmaster Talker

Latest Blog Post:
How Do You Find Music Online ?
Posts: 522
Name: Gabe Solomon
Location: Romania
I think that preg replace is the best way to go because i am betting that the Y part will not be the same every time ... and for str_replace the Y part must be the same every time
__________________
If you like my posts ... TK is appreciated:)
Web Directory | Blog
solomongaby is offline
Reply With Quote
View Public Profile Visit solomongaby's homepage!
 
Old 04-29-2007, 02:38 PM Re: help with preg_replace
kaisellgren's Avatar
Extreme Talker

Posts: 214
Name: Kai Sellgren
Location: Finland
Quote:
Originally Posted by solomongaby View Post
I think that preg replace is the best way to go because i am betting that the Y part will not be the same every time ... and for str_replace the Y part must be the same every time
That is what I was thinking about.

My regex adds 'X' before the content between " and " after matched text 'src='.
__________________
Adept Web Community
kaisellgren is offline
Reply With Quote
View Public Profile Visit kaisellgren's homepage!
 
Old 04-29-2007, 03:12 PM Re: help with preg_replace
tripy's Avatar
Fetchez la vache!

Posts: 2,055
Name: Thierry
Location: In the void
I really should take some time to get past the basis with regexp, but this look sooooo obscure to me that I never had the courage to....
And always succedded in using more str_replace, and it really keeps more readable, I think.

Your regexp is great, but if someone asked me to debug it, I would be lost.
Espacially the "'$1X$2$3'" part.
A backreference, isn't it ?
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 04-29-2007, 04:28 PM Re: help with preg_replace
kaisellgren's Avatar
Extreme Talker

Posts: 214
Name: Kai Sellgren
Location: Finland
Quote:
Originally Posted by tripy View Post
I really should take some time to get past the basis with regexp, but this look sooooo obscure to me that I never had the courage to....
And always succedded in using more str_replace, and it really keeps more readable, I think.

Your regexp is great, but if someone asked me to debug it, I would be lost.
Espacially the "'$1X$2$3'" part.
A backreference, isn't it ?
Yup I think. I don't know the terms, but I think you are right

$1 is the left part before Y, $3 is the right part after Y and $2 is the Y part or whatever you have placed for the letter Y.
__________________
Adept Web Community
kaisellgren is offline
Reply With Quote
View Public Profile Visit kaisellgren's homepage!
 
Reply     « Reply to help with preg_replace
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15347 seconds with 12 queries