Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Trim a text after first occurence of a specified string

Posted on 2006-04-13
15
Medium Priority
?
291 Views
Last Modified: 2008-03-10
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!
 
0
Comment
Question by:athanasius296
15 Comments
 
LVL 1

Expert Comment

by:phenixfilms
ID: 16447763
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
 
LVL 8

Expert Comment

by:KennyTM
ID: 16447965
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
 
LVL 11

Expert Comment

by:star_trek
ID: 16448292

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

$content = $matches[0][1]; //This will output the content
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:athanasius296
ID: 16448458
'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
 
LVL 11

Expert Comment

by:star_trek
ID: 16448481
If you may a multiline content use, notice m
preg_match_all("/(.*)((<p>&nbsp;<\/p>)|$)/Uim", $content, $matches, PREG_SET_ORDER);
0
 

Author Comment

by:athanasius296
ID: 16448529
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
 
LVL 11

Expert Comment

by:star_trek
ID: 16448557
athanasius296, Can you post how the content is?
0
 

Author Comment

by:athanasius296
ID: 16448597
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
 

Author Comment

by:athanasius296
ID: 16448610
and, of course, after <P>&nbsp;</p> a text continues.
0
 

Author Comment

by:athanasius296
ID: 16448687
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
 
LVL 11

Expert Comment

by:star_trek
ID: 16448744
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
 
LVL 11

Expert Comment

by:star_trek
ID: 16448799
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
 

Author Comment

by:athanasius296
ID: 16448803
Cuts off everything - nothing is being displayed.
0
 
LVL 11

Accepted Solution

by:
star_trek earned 1000 total points
ID: 16448864
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
 

Author Comment

by:athanasius296
ID: 16449231
Perfect! Thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question