By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,642 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,642 IT Pros & Developers. It's quick & easy.

Testing for substring

P: n/a
I have a news headline grabbed from another page in the same site. The data
is captured like so:
$post=mysql_fetch_row($res2); $post=$post[0];

I then do a strlen analysis on $post, chop it off at $maxlen, and add
ellipses. This all works fine. The problem occurs if $post contains <pre>
data and the headline is chopped before the closing </pre> tag. The missing
</pre> ruins the page layout for everything that follows.

* The <pre> tag may or may not be present
* If <pre> is present, it may or may not be at the beginning of the string

I have looked at substr, strstr and strpos, but none of them seem to quite be
what I'm looking for. Advice please on how best to test for this.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2003-11-06, <> <lo******@44.255.255.255> wrote:
I have a news headline grabbed from another page in the same site. The data
is captured like so:
$post=mysql_fetch_row($res2); $post=$post[0];

I then do a strlen analysis on $post, chop it off at $maxlen, and add
ellipses. This all works fine. The problem occurs if $post contains <pre>
data and the headline is chopped before the closing </pre> tag. The missing
</pre> ruins the page layout for everything that follows.

* The <pre> tag may or may not be present
* If <pre> is present, it may or may not be at the beginning of the string

I have looked at substr, strstr and strpos, but none of them seem to quite be
what I'm looking for. Advice please on how best to test for this.


Wat are you looking for anyway?
To determine if there is a "<pre>" in your variable, strstr is what you
need.

If you only want to append a "</pre>" to the en of that variable,
just concatenate the "</pre>" to your variable.

If you want to add everything between "<pre>" en "</pre>", you might
want to use strstr to determine where "</pre>" is located, and use that
result to redifine your maxlen.

--
Visit my website at http://home.scarlet.be/~vwadsl
My jabber handle is aa*****@jabber.org
Jul 17 '05 #2

P: n/a
*** <> wrote/escribió (Thu, 06 Nov 2003 06:53:59 GMT):
I then do a strlen analysis on $post, chop it off at $maxlen, and add
ellipses. This all works fine. The problem occurs if $post contains <pre>
data and the headline is chopped before the closing </pre> tag. The missing
</pre> ruins the page layout for everything that follows.


When dealing with text entered by third parties it's normally better to
ignore original format. If it's supposed to be plain text, you can user
htmlentities() to print it. If it's supposed to be HTML you can use a
combination of strip_tags() and preg_replace().

--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #3

P: n/a
"Tim Van Wassenhove" <eu**@pi.be> wrote in message
news:bo*************@ID-188825.news.uni-berlin.de...
On 2003-11-06, <> <lo******@44.255.255.255> wrote:

To determine if there is a "<pre>" in your variable, strstr is what you
need.


That was so painfully obvious that I'm embarrassed to have asked the
question.

Here's what I ended up with:
$pre_test1 = strstr($post_text, '<pre>'); $pre_test2 = strstr($post,
'</pre>');
if (!empty($pre_test1) && empty($pre_test2)) $post_text=$post.'</pre>';

Jul 17 '05 #4

P: n/a
"<>" <lo******@44.255.255.255> wrote in message
news:gW******************@twister.socal.rr.com...
"Tim Van Wassenhove" <eu**@pi.be> wrote in message
news:bo*************@ID-188825.news.uni-berlin.de...
On 2003-11-06, <> <lo******@44.255.255.255> wrote:

To determine if there is a "<pre>" in your variable, strstr is what you
need.


That was so painfully obvious that I'm embarrassed to have asked the
question.

Here's what I ended up with:
$pre_test1 = strstr($post_text, '<pre>'); $pre_test2 = strstr($post,
'</pre>');
if (!empty($pre_test1) && empty($pre_test2)) $post_text=$post.'</pre>';


Hit 'Send' by accident. Here's what I *really* ended up with:
$pre_test1 = strstr($post, '<pre>'); $pre_test2 = strstr($post, '</pre>');
if (!empty($pre_test1) && empty($pre_test2)) $post=$post.'</pre>';

Thanks for your help, Tim!
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.