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!
 
athanasius296Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phenixfilmsCommented:
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?
0
KennyTMCommented:
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>'));
0
star_trekCommented:

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

$content = $matches[0][1]; //This will output the content
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

athanasius296Author Commented:
'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.
0
star_trekCommented:
If you may a multiline content use, notice m
preg_match_all("/(.*)((<p>&nbsp;<\/p>)|$)/Uim", $content, $matches, PREG_SET_ORDER);
0
athanasius296Author Commented:
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.
0
star_trekCommented:
athanasius296, Can you post how the content is?
0
athanasius296Author Commented:
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>



0
athanasius296Author Commented:
and, of course, after <P>&nbsp;</p> a text continues.
0
athanasius296Author Commented:
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.
0
star_trekCommented:
same as above instead of m use 's' as in the following
preg_match_all("/(.*)((<p>&nbsp;<\/p>)|$)/Uis", $content, $matches, PREG_SET_ORDER);
0
star_trekCommented:
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.

0
athanasius296Author Commented:
Cuts off everything - nothing is being displayed.
0
star_trekCommented:
I'm sorry it is preg_match, the above has preg_match_all

preg_match("/(.*)((<p>&nbsp;<\/p>)|$)/Uis", $content, $matches);
$content = $matches[1]; //contains the string you need.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
athanasius296Author Commented:
Perfect! Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.