Problem while using regular expression

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

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Problem while using regular expression

Post by eshban »

Hello,

I have facing a problem. Kindly help

I have a Regular expression

Code: Select all

$pattern = '!<img src="([^"]+)"!';
I have a php code which reads a html file, then that regular expression searches <img> tag
Becoz according to my requirement i have to change the default path found in <img> tag.

Here is another regular expression

Code: Select all

$replace = '<img src=images/'."$1";
This is used to replace the current path found in an html document.


Suppose, i have an html file, in that html file there are two <img> tags and those have

these paths like

Code: Select all

<img src = "abc/first_pic.jpg">
and

Code: Select all

<img src = "admin/img/picture.jpg>"
My Requirement is to change the path "abc/first_pic.jpg" to "images/first.jpg"
and
"admin/img/picture.jpg" to "images/picture.jpg"

Means in every requirement i use this path i.e. 'images/anypicturefile.jpg'

For that purpose i must change my current regular expression which is
$replace = '<img src=images/'."$1";

Can anybody help me to change this regular expression $replace = '<img src=images/'."$1";
,so that it fullfills the above requirement
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

I think you dont need to change anything. As per my understanding
the following pattern should be

Code: Select all

$pattern = '!<img src="([^"]+)"!';
actually this.

Code: Select all

$pattern = '!<img src="([^"].+)"!';
In case my assumption above is right, then I think you need not change anything as the other pattern

Code: Select all

$replace = '<img src=images/'."$1";
replaces anything that comes as with any reference will be directed to images directory. Hope Im clear
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

No man i have just used your regular expression

Code: Select all

$pattern = '!<img src="([^"].+)"!'; 
instead of mine which is

Code: Select all

$pattern = '!<img src="([^"]+)"!'; 
To test it i use this code

Code: Select all

preg_match_all('!<img src="([^"].+)"!', $contents, $matches);  


	echo '<pre>';
	print_r($matches[1]); 								
	echo '</pre>';	


And this array prints result as :

Code: Select all


Array
(
    [0] => SamplePictures/Blue hills.jpg" width="80" height="60
    [1] => SamplePictures/Sunset.jpg" width="80" height="60
    [2] => SamplePictures/Water lilies.jpg" width="80" height="60
    [3] => SamplePictures/Winter.jpg" width="80" height="60
)
If your regulart expression is correct then it must print this result

Code: Select all

Array
(
    [0] => Blue hills.jpg" width="80" height="60
    [1] => Sunset.jpg" width="80" height="60
    [2] => Water lilies.jpg" width="80" height="60
    [3] => Winter.jpg" width="80" height="60
)

hope you got my point. So please help

Thanks for your time
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Firstly I got your question wrong. Also preg_match_all only matches, it doesnt replace, so you need to use either preg_replace(0% this) or preg_split. To write the next code I need what kind of possiblities are there in your

Code: Select all

<img src=??>
tag
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

Thanks for your help

In

Code: Select all

<img>
tag i just need image name.

like

Code: Select all

<img src = "mypic.jpg">
instead of

Code: Select all

<img src = "abc/img/mypic.jpg">
hope you got my point[/syntax]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

what you want to use is the "ungreedy" modifier:

Code: Select all

preg_match_all('!<img.*?src="(.*?)".*?/?>!',$src,$matches);
You can then use basename() to get just the filename of each tag.
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

thanks for your help. I will try this code very sooon.

I just confirm one thing. My requirement is that
i have a string like

Code: Select all

<img src = "img/my_folder/picture.jpg>
I have made a regular expression which searches for the whole <img> tag. But the problem is that, my regular expression stores the whole path as

"img/my_folder/picture.jpg"

I only want that my regular expression only capture the file name as 'picture.jpg' instead of the whole path. Hope you got my point.

So is the last reply is correct according to this requirement.

Thanks
Eshban[/syntax]
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

That's where basename() comes in... look it up.
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

your statement

Code: Select all

preg_match_all('!<img.*?src="(.*?)".*?/?>!',$src,$matches);
returns the same result as mine.

like it show full path as "sample/abc.jpg"

i just need abc.jpg

Eshban
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'm not a regex expert but try

Code: Select all

<?php
$pattern = '!<img\s+src\s*=\s*"[^"]*?([^/"]+)"!i';

preg_match($pattern, '<Img src = "anyfile.jpg">', $m);
echo $m[1], "<br />\n";

preg_match($pattern, '<img src = "abc/www/image.jpg">', $m);
echo $m[1], "<br />\n";
?>
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

eshban wrote:your statement.....returns the same result as mine.
No, it doesn't. Look closer at what your regexp is returning

Have you even bothered looking at what basename() does? We're not going to write all your code for you, you'll have to do some of the work yourself...
Post Reply