limit echo in php to certain word in string
Hello
I am a bit stuck with a php problem. I have a string of text that can be of a variable length eg " Large and luxurious 5th floor 2 bed furnished apartment in a fantastic historic building close to the City Centre and Waterfront. More details and 8 photos Save property Contact agent " The text in the first 2 lines can be variable in length. The "More details... etc" is standard text. I want to display my string up to, but not including, the word "More." I am looking (I think) for something like : echo $mystring (up to the word "More") << this is the bit I can't work out. Thanks |
explode on newline, then get element 1 of the exploded array
|
I should have been clearer. I am taking the text string from another page using :
$content = file_get_contents('http://www.......); I have then (somehow http://ubuntuforums.org/images/smilies/icon_smile.gif ) managed to get the text string I want using : $start_pointer = strpos($content, '<ol id="summaries"'); $end_pointer = strpos($content, '</ol>', $start_pointer); $properties_content_length = $end_pointer - $start_pointer; $properties = substr($content, $start_pointer, $properties_content_length); $property_list = explode('<li class="regular',$properties); $property_nbr = count($property_list); echo '<li class="'; echo strip_tags($property_list[ rand(1, $property_nbr)-1], '<p><img><li><span><div><h2>'); This works fine, except that I want to limit the "echo $property_list" to a certain word eg the "More" in my string below. " Large and luxurious 5th floor 2 bed furnished apartment in a fantastic historic building close to the City Centre and Waterfront. More details and 8 photos Save property Contact agent " How and where do I explode again? Thanks |
you will need to use a combination of substr() and strripos()
something like this PHP Code:
you can also condense it, but kept it open to show you what was happening exactly. -P |
The solution (for me) was :
$home_properties=strip_tags($property_list[ rand(1, $property_nbr)-1], '<p><img><li><span><div><h2>'); $lines=explode("More details",$home_properties); echo $lines[0]; Thanks ghostdog74 & pendal |
All times are GMT -5. The time now is 05:41 PM. |