Page 1 of 1

Removing everything after " from a string

Posted: Wed Oct 29, 2008 7:33 pm
by coolbeansdude51
Hello everyone,

I am wondering if someone could help me out. I am trying to just grab a url from a html file. I can search the file easy enough but then once I get the url I can't cut it down to just the url.

The code is below:

Code: Select all

 
<?PHP
 
$source = file_get_contents('http://www.apple.com/trailers/independent/fearsofthedark/');
$search = "<img src=\"";
$newText = substr($source,strpos($source, $search)+strlen($search), 100);
print $newText;
?>
 
It out puts this:
http://images.apple.com/trailers/independent/images/fearsofthedark_200809251636.jpg" width="134" hei

I need it to be cut at " and just have http://images.apple.com/trailers/independent/images/fearsofthedark_200809251636.jpg

Anyone have any ideas?

Thanks!

Re: Removing everything after " from a string

Posted: Wed Oct 29, 2008 7:48 pm
by requinix
Have a look at strtok.

Re: Removing everything after " from a string

Posted: Wed Oct 29, 2008 7:53 pm
by coolbeansdude51
HA!

Success!

Thanks! It now looks like this!

Code: Select all

 
<?PHP
 
$source = file_get_contents('http://www.apple.com/trailers/independent/fearsofthedark/');
 
$search = "<img src=\"";
 
$newText = substr($source,strpos($source, $search)+strlen($search));
 
$strip = strtok($newText, '"');
 
print $strip;
?>
 

Re: Removing everything after " from a string

Posted: Wed Oct 29, 2008 7:59 pm
by Stryks
I'd use regex for something like that.

Something like ...

Code: Select all

$source = file_get_contents('http://www.apple.com/trailers/independent/fearsofthedark/');
 
$matches_found = preg_match('/<img[^>]*src="(?P<url>[^"]*)"[^>]*>/i', $source, $matches);
$result = $matches['url'];
echo $result; 
 
That will just list the url found, but you get the idea. In your sample page, it looks like you'll always want the first item ($result[0]), but you might want to make sure that it will always be so, otherwise you might wind up with spacer images or the like.

That's untested by the way, but it should work I think.

Cheers

EDIT: Hehehe ... now it's tested and should work, though it can be made a little more useful

Re: Removing everything after " from a string

Posted: Wed Oct 29, 2008 8:08 pm
by coolbeansdude51
That's a great idea.

It code that you provided didn't work for me.

Any suggestions?

Re: Removing everything after " from a string

Posted: Wed Oct 29, 2008 8:13 pm
by Stryks
Yeah ... I hadn't tested it, but the above should work now.

Let me know if it doesn't.

Re: Removing everything after " from a string

Posted: Wed Oct 29, 2008 8:24 pm
by coolbeansdude51
Awesome!

Thanks. Works great.

Re: Removing everything after " from a string

Posted: Wed Oct 29, 2008 8:41 pm
by coolbeansdude51
I am trying to use the preg_match function to search for this inside of a file for <a class="face"

I can't seem to format the preg_match correctly.

I tried this:

$matches_found = preg_match('/<a[^>]*class=\"face(?P<url>[^"]*)"[^>]*>/i', $source, $matches);
$matches_found = preg_match('/<a[^>]*class=[^"]face(?P<url>[^"]*)"[^>]*>/i', $source, $matches);

I couldn't figure it out.

Any suggestions? Or other sources that might help?

Thanks for the help so far.

Re: Removing everything after " from a string

Posted: Wed Oct 29, 2008 11:35 pm
by Stryks
Pretty much the same code with ...

Code: Select all

preg_match('/<a[^>]*(?:class="face")?href="(?P<url>[^"]*)"(?:class="face")?[^>]*>/i', $source, $matches)
... should do it.

I had to reference class="face" twice in case they are out of sequence, and obviously I haven't allowed for any difference of format (such as single quotes, spaces, etc).

Also, as per the previous example, it only returns the first value found. You could pull all values using the same regex and preg_match_all.

Hope that helps. :)