LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   limit echo in php to certain word in string (https://www.linuxquestions.org/questions/programming-9/limit-echo-in-php-to-certain-word-in-string-772467/)

esteeven 11-30-2009 10:18 AM

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

ghostdog74 11-30-2009 10:42 AM

explode on newline, then get element 1 of the exploded array

esteeven 11-30-2009 11:04 AM

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

pendal 11-30-2009 11:12 AM

you will need to use a combination of substr() and strripos()

something like this

PHP Code:

$string some stuff there and then your thing... More Detailsetc;


//find the more details position

$position strripos($string,"More Details"); 

//then get the string up to the location of the above position

$remaining substr($string,0,$position); 

that should get it close, you may need to tweak it a little to get the Char position just right.

you can also condense it, but kept it open to show you what was happening exactly.

-P

esteeven 11-30-2009 01:12 PM

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.