Page 1 of 1

Extracting certain text from PHP string...

Posted: Thu Jun 15, 2006 1:54 pm
by ryanrbftp
Using PHP, If I had the following string:
original and flight model "Robert Versluys" repaint in Air Dolomiti by "Massimo Grassi" full 3 d texture,movimg parts,night effects<br><img src="/images/aircraft/fs2002civiljets/airdccrj2.jpg" width="150" height="112">
How could I extract only
/images/aircraft/fs2002civiljets/airdccrj2.jpg
and continue using it im my script? Please bear in mind that the image files change, as well as the DIR's for the images, so perhaps start extracting with the starting point of
<img
and finishing with
>
Any ideas guys?

Posted: Thu Jun 15, 2006 2:36 pm
by printf
Just use any of the preg_ functions that best fit your needs....

example... (preg_match_all) match more than one occurrence

Code: Select all

<?
	$data = file_get_contents ( 'http://forums.devnetwork.net/viewtopic.php?t=50148' );

	$regx = "|src\=\"?'?`?([[]:?=&@/._-]+)\"?'?`?|i";

	preg_match_all ( $regx, $data, $src );

	// strip array(0), don't need it

	$src = $src[1];

	print_r ( $src );
?>
Why is this forum stripping * :alnum: * from the inner set of brackets

pif!

Posted: Thu Jun 15, 2006 2:54 pm
by ryanrbftp
Thank you for your reply.

This is my code:

Code: Select all

$data = $row['description'];

        $regx = "|src\=\"?'?`?([[]:?=&@/._-]+)\"?'?`?|i";

        preg_match_all ( $regx, $data, $src );

        // strip array(0), don't need it

        $src = $src[1];

	echo "<g:image_link>$src</g:image_link>\n";
However, all thats getting returned is this:
<g:image_link>Array</g:image_link>
Any ideas?

Posted: Thu Jun 15, 2006 3:26 pm
by printf
Because this forum is messing up the PHP REGEX, like it does for so many other things!


replace the other regex with this!

Code: Select all

$regx = "|src\=\"?'?`?([[:alnum:]:?=&@/._-]+)\"?'?`?|i";

pif!

Posted: Thu Jun 15, 2006 3:35 pm
by ryanrbftp
Still the same, just comes up with "Array". I'm entering :0123456789: and have also tried without the colons. Any ideas?

Posted: Thu Jun 15, 2006 3:44 pm
by RobertGonzalez
printf wrote:Because this forum is messing up the PHP REGEX, like it does for so many other things!
It is not the forum, it is the GeSHi plugin for syntax highlighting. The problem has been reported and is being looked (reported for sure, looked at I think).

Posted: Thu Jun 15, 2006 3:46 pm
by RobertGonzalez
ryanrbftp wrote:Still the same, just comes up with "Array". I'm entering :0123456789: and have also tried without the colons. Any ideas?
Do a print_r() of the array to see what is in it.

Posted: Thu Jun 15, 2006 3:49 pm
by printf
Where are you putting this => :0123456789:, I hope not in the regex!

Code: Select all

<?
	$data = file_get_contents ( 'http://forums.devnetwork.net/viewtopic.php?t=50148' );

	$regx = "|src\=\"?'?`?([[:alnum:]:?=&@/._-]+)\"?'?`?|i";

	preg_match_all ( $regx, $data, $src );

	$src = $src[1];

	print_r ( $src );

?>

pif!

Posted: Thu Jun 15, 2006 3:55 pm
by ryanrbftp

Code: Select all

$data = $row['description'];

        $regx = "|src\=\"?'?`?([[:0123456789:]:?=&@/._-]+)\"?'?`?|i";

        preg_match_all ( $regx, $data, $src );


        $src = $src[1];

	echo "<g:image_link>$src</g:image_link>\n";

Posted: Thu Jun 15, 2006 3:59 pm
by ryanrbftp
Sorry, I have changed my code to the following, but still nothing:

Code: Select all

$data = $row['description'];

        $regx = "|src\=\"?'?`?([[:alnum:]:?=&@/._-]+)\"?'?`?|i"; 

        preg_match_all ( $regx, $data, $src );


        $src = $src[1];

	echo "<g:image_link>$src</g:image_link>\n";
Printr of array shows:
Array
(
[0] => /images/aircraft/fs2002civiljets/737smokelarge.jpg
)
If I changed $src = $src[1]; to $src = $src[0], would that work?

Posted: Thu Jun 15, 2006 4:03 pm
by ryanrbftp
No, changing 1 to 0 didnt work either. It's because its printing only the first line of the output in my script. How can I get it to print only the

Code: Select all

/images/aircraft/fs2002civiljets/737smokelarge.jpg

Posted: Thu Jun 15, 2006 4:03 pm
by RobertGonzalez
Try $src[1][0] and see what that does.

Posted: Thu Jun 15, 2006 4:06 pm
by ryanrbftp
no, $src = $src[0][1] didnt even print anything.

Posted: Thu Jun 15, 2006 4:08 pm
by RobertGonzalez
Sorry, I edited the last post and my proxy server is slower than crud. Try $src[1][0].

Also, the PHP Manual on preg_match_all has some pretty useful examples and how to capture what is the resulting array.

Posted: Thu Jun 15, 2006 4:11 pm
by ryanrbftp
That worked a treat. Now i've just gotta add the http://domainname.com before the image file!

Thanks guys.