cataiin Posted March 28, 2014 Share Posted March 28, 2014 (edited) 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 March 28, 2014 by cataiin Quote Link to comment https://forums.phpfreaks.com/topic/287374-return-all-strstr-results/ Share on other sites More sharing options...
jazzman1 Posted March 28, 2014 Share Posted March 28, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287374-return-all-strstr-results/#findComment-1474349 Share on other sites More sharing options...
cataiin Posted March 28, 2014 Author Share Posted March 28, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/287374-return-all-strstr-results/#findComment-1474350 Share on other sites More sharing options...
requinix Posted March 28, 2014 Share Posted March 28, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/287374-return-all-strstr-results/#findComment-1474351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.