Extracting certain text from PHP string...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ryanrbftp
Forum Newbie
Posts: 8
Joined: Thu Jun 15, 2006 1:47 pm

Extracting certain text from PHP string...

Post 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?
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post 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!
ryanrbftp
Forum Newbie
Posts: 8
Joined: Thu Jun 15, 2006 1:47 pm

Post 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?
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post 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!
ryanrbftp
Forum Newbie
Posts: 8
Joined: Thu Jun 15, 2006 1:47 pm

Post by ryanrbftp »

Still the same, just comes up with "Array". I'm entering :0123456789: and have also tried without the colons. Any ideas?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post 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!
ryanrbftp
Forum Newbie
Posts: 8
Joined: Thu Jun 15, 2006 1:47 pm

Post 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";
ryanrbftp
Forum Newbie
Posts: 8
Joined: Thu Jun 15, 2006 1:47 pm

Post 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?
ryanrbftp
Forum Newbie
Posts: 8
Joined: Thu Jun 15, 2006 1:47 pm

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Try $src[1][0] and see what that does.
ryanrbftp
Forum Newbie
Posts: 8
Joined: Thu Jun 15, 2006 1:47 pm

Post by ryanrbftp »

no, $src = $src[0][1] didnt even print anything.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
ryanrbftp
Forum Newbie
Posts: 8
Joined: Thu Jun 15, 2006 1:47 pm

Post by ryanrbftp »

That worked a treat. Now i've just gotta add the http://domainname.com before the image file!

Thanks guys.
Post Reply