Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

problem with preg_match

Posted on 2011-05-06
11
Medium Priority
?
282 Views
Last Modified: 2012-05-11
Hi.

the following code:
 
<? $subject= ' ?><!--blablablabla-w278px-x-h227px-->
<img src="cms/uploaded/images/Desert.jpg" alt="blablabla" width="1024" height="768" /><br />
<!--blablablabla--><? '
	

	preg_match('@(?:src="[^"]*?/)(cms/[^"]*)@', $subject, $matches);
	$url = $matches[1];
	echo $url; 
?>

Open in new window


returns this: Notice: Undefined offset: 1 in /home/blablabla/web/cms/scripts.php on line 34

it should return the url: "cms/uploaded/images/Jellyfish.jpg"

what is going wrong?
0
Comment
Question by:peps03
  • 5
  • 5
11 Comments
 
LVL 14

Expert Comment

by:svgmuc
ID: 35705816
The first array element should be 0:

$url = $matches[0];
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 35705829
http://www.laprbass.com/RAY_temp_peps03.php
Outputs: string(30) "cms/uploaded/images/Desert.jpg"
<?php // RAY_temp_peps03.php
error_reporting(E_ALL);
echo "<pre>";

$subject= ' ?><!--blablablabla-w278px-x-h227px-->
<img src="cms/uploaded/images/Desert.jpg" alt="blablabla" width="1024" height="768" /><br />
<!--blablablabla--><? ';

$regex
= '#'      // REGEX DELIMITER
. 'src="'  // BEGIN SEARCH STRING
. '(.*?)'  // GROUP
. '"'      // END SEARCH STRING
. '#'      // REGEX DELIMITER
. 'i'      // CASE-INSENSITIVE HTML
;

preg_match($regex, $subject, $matches);
var_dump($matches[1]);

Open in new window

0
 

Author Comment

by:peps03
ID: 35705838
thanks for your reaction svgmuc!

but then it returns:
Notice: Undefined offset: 0 in /home/vg005web11/12/31/1003112/web/cms/scripts.php on line 34
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35705846
I often find that it is easier to write a REGEX if you put the different pieces on separate lines.  Then you can look at them and think about them with a little more clarity than when the pieces are all jumbled together on a single line.

But that said, the data set posted above will never return the url: "cms/uploaded/images/Jellyfish.jpg" -- the data set does not contain the word Jellyfish.  Did you want us to show you how to translate Desert into Jellyfish?
0
 

Author Comment

by:peps03
ID: 35705859
thanks Ray!

i see it now outputs:
string(30) "cms/uploaded/images/Desert.jpg"

how do i remove the "string(30)" ?
0
 

Author Comment

by:peps03
ID: 35705879
haha, excuse me! was a copy past error. it should return desert.
was using standard windows sample pictures for testing.

and indeed, thanks for the REGEX explanation. it is easier that way!

what does: REGEX DELIMITER mean?
0
 

Author Comment

by:peps03
ID: 35705886
i got it, change var_dump to echo ;)

thanks!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35705948
Yep - the string(30) is just part of the var_dump() output.  I like to use var_dump() when I am debugging because if you use echo and the input is an array or object you might not see what you expected.  Var_dump() tells you the data type and size as well as any printable contents of the variable.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35705967
what does: REGEX DELIMITER mean?

All regular expressions in PHP have to contain matching start and stop characters.  In this case it is the has mark #.  The flags (case-insensitive, multi-line, etc.) come after the ending delimiter.
0
 

Author Closing Comment

by:peps03
ID: 35705975
Thanks again Ray!

Now i can continue my work!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35705989
Thanks for the points - best of luck with your project! ~Ray
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question