Page 1 of 1

Problem while using regular expression

Posted: Wed Jan 24, 2007 6:03 am
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

Posted: Wed Jan 24, 2007 6:15 am
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

Posted: Wed Jan 24, 2007 6:32 am
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

Posted: Wed Jan 24, 2007 7:22 am
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

Posted: Wed Jan 24, 2007 7:35 am
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]

Posted: Wed Jan 24, 2007 9:58 am
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.

Posted: Wed Jan 24, 2007 12:02 pm
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]

Posted: Wed Jan 24, 2007 6:02 pm
by Kieran Huggins
That's where basename() comes in... look it up.

Posted: Thu Jan 25, 2007 3:26 am
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

Posted: Thu Jan 25, 2007 4:35 am
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";
?>

Posted: Thu Jan 25, 2007 11:11 am
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...