473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Testing for substring

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
4 2762
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
*** <> 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
"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
"<>" <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Steve Jorgensen | last post by:
Over the last few days, I've come up with and played with what's turned out to be a really useful technique that helps to test and document stored procedures. Pretty much any time you write a...
7
by: Radhika Sambamurti | last post by:
Hi, I've written a substring function. The prototype is: int substr(char s1, char s2) Returns 1 if s2 is a substring of s1, else it returns 0. I have written this program, but Im sure there is an...
4
by: Michael McCarthy | last post by:
I want to test for the ~possible~ occurance of a string within another string... IndexOfAny gives wildly odd results, possibly because it's expecting the string to be there... This is probably...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
5
by: btober | last post by:
I can't seem to get right the regular expression for parsing data like these four sample rows (names and addresses changed to ficticious values) from a text-type column: Yolanda Harris, 38, of...
4
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
11
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the...
9
by: tinnews | last post by:
What's the neatest and/or most efficient way of testing if one of a set of strings (contained in a dictionary, list or similar) is a sub-string of a given string? I.e. I have a string delivered...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.