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> </P>' exist, and cut everything after the first instance of the string.
Note: '<P>' can also be typed lowercase '<p>';
Any thoughts?
Thanks!
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
$content = implode(" ", $content_array);
Now, I want to find out if a string '<P> </P>' exist, and cut everything after the first instance of the string.
Note: '<P>' can also be typed lowercase '<p>';
Any thoughts?
Thanks!
If phenixfilms is right, then you could try the following. Before explode()-ing,
$content = substr($content, 0, strpos(strtolower($content ), '<p> </p>'));
for PHP-5, you can simply use
$content = substr($content, 0, stripos($content, '<p> </p>'));
$content = substr($content, 0, strpos(strtolower($content
for PHP-5, you can simply use
$content = substr($content, 0, stripos($content, '<p> </p>'));
preg_match_all("/(.*)((<p>
$content = $matches[0][1]; //This will output the content
ASKER
'phenixfilms ' is right: that's what I want.
I tried:
$content = substr($content, 0, strpos(strtolower($content ), '<p> </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.
I tried:
$content = substr($content, 0, strpos(strtolower($content
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> <\/p >)|$)/Uim" , $content, $matches, PREG_SET_ORDER);
preg_match_all("/(.*)((<p>
ASKER
OK. My second attempt:
preg_match_all("/(.*)((<p> <\/p >)|$)/Uim" , $content, $matches, PREG_SET_ORDER);
still displays nothing, but:
$content = substr($content, 0, strpos(strtolower($content ), '<p> </p>'));
works.
Works, however, only if the string '<p> </p>' is in the text. If not - nothing is displayed.
preg_match_all("/(.*)((<p>
still displays nothing, but:
$content = substr($content, 0, strpos(strtolower($content
works.
Works, however, only if the string '<p> </p>' is in the text. If not - nothing is displayed.
athanasius296, Can you post how the content is?
ASKER
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> </p>
<p align="justify">Technology
<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> </p>
ASKER
and, of course, after <P> </p> a text continues.
ASKER
I just noticed, that if in the first 300 characters '<p> </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> <\/p >)|$)/Uis" , $content, $matches, PREG_SET_ORDER);
preg_match_all("/(.*)((<p>
You can also use preg_match instead of preg_match_all
preg_match_all("/(.*)((<p> <\/p >)|$)/Uis" , $content, $matches);
$content = $matches[1]; //contains the string you need.
preg_match_all("/(.*)((<p>
$content = $matches[1]; //contains the string you need.
ASKER
Cuts off everything - nothing is being displayed.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect! Thank you!
so...
hello my name is sam <P> </P> and i live in a can
would become
hello my name is sam
correct?