Reply
problems creating regular expression for preg_replace
Old 03-22-2006, 07:42 AM problems creating regular expression for preg_replace
numbenator's Avatar
Ultra Talker

Posts: 411
Location: London
Hi All,
Im getting confused.

Could someone help me to create expressions for preg_replace which i am really struggling with

1) <H2>Foreign News &nbsp;<IMG height=40
src="fullPage_files/foreign.gif" width=48 border=0></H2>

I require to convert Foreign News to uppercase but other titles as well that are in the same format. (the image is throwing my preg_replace I am sure

I am using ($buffer all the html code extracted from file)

$buffer = preg_replace("/<(h[0-9])>(.*?)<\/h[0-9]>/ie", "'<\\1>'.strtoupper('\\2').'</\\1>'", $buffer);

the above works if text is only in <H2>test</H2> and not when there is a image in the H tags as well

ALSO

2. I wish to replace an image with an * in the file.

the image in html is <IMG height=9 alt="" src="fullPage_files/darkerbullet.gif"
width=9 border=0>

when I try to use a replace I think the " in the img tag are throwing the replace. How do i deal with this.

Cheers for any help you can offer me.
__________________
www.mastech-solutions.com
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
When You Register, These Ads Go Away!
Old 03-22-2006, 01:57 PM Re: Using preg_replace
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Your first one is doing exactly what you meant - capitalising all the things between the h2s, including the image tag and the non-breaking space - If you don't want it capitalised you'll have to move it outside the h2, or re-write the regex to ignore things inside of other tags.

You could do the second one with just a simple text replacement:

PHP Code:
$str2 str_replace("<IMG height=9 alt=\"\" src=\"fullPage_files/darkerbullet.gif\" width=9 border=0>","*",$str); 
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)

Last edited by 0beron : 03-22-2006 at 02:07 PM.
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-24-2006, 12:22 AM Re: problems creating regular expression for preg_replace
Super Talker

Posts: 144
first, strip the image tags, then convert to uppercase.

$buffer = preg_replace("/<img.*?>/i", "*", $buffer);

$buffer = preg_replace("/<(h[0-9])>(.*?)<\/h[0-9]>/ie", "'<\\1>'.strtoupper('\\2').'</\\1>'", $buffer);


are you going to have more tags inside the H tag?
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Reply     « Reply to problems creating regular expression for 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.14398 seconds with 12 queries