LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2009, 10:18 AM   #1
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Rep: Reputation: 52
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
 
Old 11-30-2009, 10:42 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
explode on newline, then get element 1 of the exploded array
 
Old 11-30-2009, 11:04 AM   #3
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Original Poster
Rep: Reputation: 52
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
 
Old 11-30-2009, 11:12 AM   #4
pendal
LQ Newbie
 
Registered: Jun 2009
Posts: 12

Rep: Reputation: 0
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
 
Old 11-30-2009, 01:12 PM   #5
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Original Poster
Rep: Reputation: 52
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
grep string with space (2 word string) casperdaghost Linux - Newbie 7 08-24-2009 02:11 AM
variable length string using GD (word wrap, carriage return, word/character count)? frieza Programming 1 02-14-2009 05:21 PM
find word between string djcham Programming 8 07-11-2008 04:37 AM
how to echo whole string to a file? dwarf007 Linux - General 5 06-20-2007 03:44 AM
Using echo to output a string to a file with forward slashs chrisk5527 Linux - General 15 06-21-2003 02:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration