Removing everything after " from a 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
coolbeansdude51
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2008 7:08 pm

Removing everything after " from a string

Post 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!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Removing everything after " from a string

Post by requinix »

Have a look at strtok.
coolbeansdude51
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2008 7:08 pm

Re: Removing everything after " from a string

Post 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;
?>
 
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Removing everything after " from a string

Post 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
coolbeansdude51
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2008 7:08 pm

Re: Removing everything after " from a string

Post by coolbeansdude51 »

That's a great idea.

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

Any suggestions?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Removing everything after " from a string

Post by Stryks »

Yeah ... I hadn't tested it, but the above should work now.

Let me know if it doesn't.
coolbeansdude51
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2008 7:08 pm

Re: Removing everything after " from a string

Post by coolbeansdude51 »

Awesome!

Thanks. Works great.
coolbeansdude51
Forum Newbie
Posts: 5
Joined: Wed Oct 29, 2008 7:08 pm

Re: Removing everything after " from a string

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Removing everything after " from a string

Post 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. :)
Post Reply