Jump to content

Return all strstr results


cataiin

Recommended Posts

I have this code:

$url = file_get_contents('http://lala.com');
$before_imdb_url = 'http://www.imdb.com/title/';
$after_imdb_url = '">';
$imdb_url = strstr(substr($url, strpos($url, $before_imdb_url) + strlen($before_imdb_url)), $after_imdb_url, true);
var_dump($imdb_url);

...result:

string(15) "tt119247"

 

...and two questions:

1. is in my case best (and fastest) way to use strstr or should I use something else - and what?

2. my code returns only one result, but there's more. I've tried with foreach but no succes:

foreach ($imdb_url as $a) {
    echo $a;
}
Warning: Invalid argument supplied for foreach().

 

what should I do?

Edited by cataiin
Link to comment
Share on other sites

Does a substr function return the result you are expected? Substr just returns some part of a string and the foreach construct provides an easy way to iterate over arrays, you can't use it here. 

 

Aww, thanks. strstr return what I want, but only one result and I need all of them.

I need to find all data between http://www.imdb.com/title/ and "> and save them to MySQL.

Link to comment
Share on other sites

So basically you're saying that strstr does exactly what you want but your problem is that it doesn't do exactly what you want?

 

strstr does not do multiple matches, as you've seen. Period. There's no way to magically make it return an array or something. So that means you have to find something else to use with/instead of strstr. In this case that's great because you should be using a different method instead: the DOM. Use DOMDocument to load the HTML and "navigate" to the different elements you want.

So unless you want to completely dismiss that idea because you still want to somehow make strstr work, how about posting the HTML you're dealing with and exactly what you're trying to get?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.