Link to home
Start Free TrialLog in
Avatar of athanasius296
athanasius296

asked on

Trim a text after first occurence of a specified string

Hi!
I use a function to get first 300 characters from the file. Then I display only 25 words from it.

$fd = fopen ($pathandfile, "r");
$content =  fread ($fd, 300);
fclose ($fd);
$content_array =  explode (" ", $content)
$content_array = array_slice($content_array, 0, 24);
$content = implode(" ", $content_array);

Now, I want to find out if a string '<P>&nbsp;</P>' exist, and cut everything after the first instance of the string.
Note: '<P>' can also be typed lowercase '<p>';

Any thoughts?
Thanks!
 
Avatar of phenixfilms
phenixfilms

It appears you want to cut everything after the occurance of  <P>&nbsp;</P>

so...

hello my name is sam <P>&nbsp;</P> and i live in a can

would become

hello my name is sam

correct?
If phenixfilms is right, then you could try the following. Before explode()-ing,

$content = substr($content, 0, strpos(strtolower($content), '<p>&nbsp;</p>'));

for PHP-5, you can simply use

$content = substr($content, 0, stripos($content, '<p>&nbsp;</p>'));

preg_match_all("/(.*)((<p>&nbsp;<\/p>)|$)/Ui", $content, $matches, PREG_SET_ORDER);

$content = $matches[0][1]; //This will output the content
Avatar of athanasius296

ASKER

'phenixfilms ' is right: that's what I want.

I tried:
$content = substr($content, 0, strpos(strtolower($content), '<p>&nbsp;</p>'));
and ti didn't work for me: it cuts off everything (nothing is displayed).
(for php 5 function I got an error).

I also tried:
preg_match_all....
with the same result.
I didn't do any changes, except placed this line(s) before explode()-ing.
If you may a multiline content use, notice m
preg_match_all("/(.*)((<p>&nbsp;<\/p>)|$)/Uim", $content, $matches, PREG_SET_ORDER);
OK. My second attempt:
preg_match_all("/(.*)((<p>&nbsp;<\/p>)|$)/Uim", $content, $matches, PREG_SET_ORDER);
still displays nothing, but:
$content = substr($content, 0, strpos(strtolower($content), '<p>&nbsp;</p>'));
works.
Works, however, only if the string '<p>&nbsp;</p>' is in the text. If not - nothing is displayed.
athanasius296, Can you post how the content is?
It's a php text file, content doesn't matter, but it is for example:
<p align="justify">Technology plays a fundamental role in the operation of today's transportation agencies. And the San Mateo County Transit District, located in California's Silicon Valley, is at the forefront of applying IT to public transportation.</p>
<p align="justify">The transit district serves one of the nation's most technically sophisticated communities. So perhaps it's no surprise that the district employs a range of innovative applications designed to improve service, enhance efficiency, boost security and attract riders. From back-end solutions designed to streamline its internal operations to strategies aimed at boosting ridership, the San Mateo County Transit District has built a reputation for innovation.</p>
<P>&nbsp;</p>



and, of course, after <P>&nbsp;</p> a text continues.
I just noticed, that if in the first 300 characters '<p>&nbsp;</p>' does not exist, then nothing is displayed. When I manually enter this string - everything works just fine. So, I thing there's some kind of IF statement needed.
same as above instead of m use 's' as in the following
preg_match_all("/(.*)((<p>&nbsp;<\/p>)|$)/Uis", $content, $matches, PREG_SET_ORDER);
You can also use preg_match instead of preg_match_all

preg_match_all("/(.*)((<p>&nbsp;<\/p>)|$)/Uis", $content, $matches);

$content = $matches[1]; //contains the string you need.

Cuts off everything - nothing is being displayed.
ASKER CERTIFIED SOLUTION
Avatar of star_trek
star_trek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect! Thank you!