473,241 Members | 1,565 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,241 software developers and data experts.

$pattern = "/.{80}/\S/"; - another regex question

When users enter urls or other long strings it can destroy the
formatting of a page. A long url, posted in a comment, can cause page
distortions that make the page unreadable, till the website owner logs in
and deletes the comment. To protect against that, I'd like to break up
long strings in the comments (anything submitted by anonymous sources).
One thing I'd like to add to the following function is the ability to
break up long strings - anything more than 80 chars should have a
space inserted. I want something like this:

..{80}

but that would include white space. How do you say "everything but
white space" in regex? I realize I'm supposed to use "/\S/" but I
don't understand how. Is this correct:

$pattern = "/.{80}/\S/";
$replaceWith = $pattern." ";

$string = eregi_replace($pattern, $replaceWith, $string);



function processStringForPublic($string, $return=0,
$class="mcParagraph") {
// 04-01-03 - this function is to replace PHP's built in nl2br()
function with something better because
// graphic designers give me grief about my code using <br> instead
of <p>.
// 03-31-03 - "for public" means we use the string to send stuff to
website visitors.
// The opposite of "forPublic" is "forEdit".

// 04-05-03 - We need to turn this into an array, using explode, so
first we need to
// get out whatever symbol we'll use to explode() the string.
$string = str_replace("`", "#x#x~~x#", $string);
$pattern = "\r?\n[ \t\r]*\n";
$string = eregi_replace($pattern, "`", $string);
$stringArray = explode("`", $string);

// Make sure the string is empty so we can refill it.
$string = "";

// 04-04-03 - Now we need a for loop to go through and give a unique
name to every paragraph.
for ($i=0; $i < count($stringArray); $i++) {
$name = makeDisplayTextFromString($stringArray[$i]);

// 04-23-03 - the $i==0 line is so that we don't print </p> as the
very first thing. Bad HTML. No validation that route! - lk
if ($i == 0) {
// 04-27-03 - is there any trouble with the id and name being the
same?
$string .= "<p id=\"$name\" name=\"$name\" class=\"$class\">";
} else {
// 04-05-03 - in this next line, the &nbsp; is needed so these
paragraphs can be used for spacing. An empty paragraph won't create a
space
$string .= "&nbsp;</p><p id=\"$name\" name=\"$name\"
class=\"$class\">";
}
$stringArray[$i] = str_replace("\n", "<br/>", $stringArray[$i]);
$string .= $stringArray[$i];
$string .= "\n\n";
}
$string .= "</p>";
$string = str_replace("#x#x~~x#", "`", $string);

$string = processHtmlTagEndings($string, "<i>");
$string = processHtmlTagEndings($string, "<b>");
$string = processHtmlTagEndings($string, "<u>");
$string = processHtmlTagEndings($string, "<em>");
$string = processHtmlTagEndings($string, "<strong>");
$string = processHtmlTagEndings($string, "<center>");
$string = processHtmlTagEndings($string, "<a ");
$string = processHtmlTagEndings($string, "<div ");
$string = processHtmlTagEndings($string, "<span ");
$string = processHtmlTagEndings($string, "<p ");
$string = processHtmlTagEndings($string, "<ul ");
$string = processHtmlTagEndings($string, "<div ");
$string = processHtmlTagEndings($string, "<span ");
$string = processHtmlTagEndings($string, "<ol ");
$string = processHtmlTagEndings($string, "<span ");
$string = processHtmlTagEndings($string, "<p ");
$string = processHtmlTagEndings($string, "<p>");
$string = processHtmlTagEndings($string, "<div>");
$string = processHtmlTagEndings($string, "<span>");
$string = processHtmlTagEndings($string, "<ul>");
$string = processHtmlTagEndings($string, "<div>");
$string = processHtmlTagEndings($string, "<span>");
$string = processHtmlTagEndings($string, "<ol>");

if ($return) {
return $string;
} else { echo $string;
}
}
Jul 17 '05 #1
5 2656
With total disregard for any kind of safety measures
lk******@geocities.com (lawrence) leapt forth and uttered:
When users enter urls or other long strings it can destroy the
formatting of a page. A long url, posted in a comment, can cause
page distortions that make the page unreadable, till the website
owner logs in and deletes the comment. To protect against that,
I'd like to break up long strings in the comments (anything
submitted by anonymous sources).


http://www.php.net/wordwrap

--
Phil Roberts | Dork Pretending To Be Hard | http://www.flatnet.net/
Jul 17 '05 #2
Phil Roberts <ph*****@HOLYflatnetSHIT.net> wrote in message news:<Xn*************************@216.196.97.132>. ..
With total disregard for any kind of safety measures
lk******@geocities.com (lawrence) leapt forth and uttered:
When users enter urls or other long strings it can destroy the
formatting of a page. A long url, posted in a comment, can cause
page distortions that make the page unreadable, till the website
owner logs in and deletes the comment. To protect against that,
I'd like to break up long strings in the comments (anything
submitted by anonymous sources).


http://www.php.net/wordwrap


Wordwrap() doesn't work. It breaks lines even if the lines don't need
to be broken. I tried it one and it lead to very ugly, jagged lines
with line breaks every 75 characters (because I'd specified 75
characters).
Jul 17 '05 #3
I noticed that Message-ID:
<da**************************@posting.google.com > from lawrence
contained the following:
http://www.php.net/wordwrap


Wordwrap() doesn't work. It breaks lines even if the lines don't need
to be broken. I tried it one and it lead to very ugly, jagged lines
with line breaks every 75 characters (because I'd specified 75
characters).


That's interesting. I didn't know about that function until after I had
written my own (yeah I know, always RTFM). Having since looked at it, it
seems that quite a few people are working on the perfect word wrapping
function. Although mine works most of the time it is not perfect.
However, it is neat and compact. I wonder if I should persevere?
function linewrap($string,$wrap,$indent)
{
$a=0;
$i=0;
while (substr($string,$a+$b,1)!=""){
$b=$wrap;
while(substr($string,$a+$b,1)!=chr(32))
{$b=$b-1;
}
$b=$b+1;
$l[$i]=substr($string,$a,$b);
$a=$a+$b;
$i++;
};
$l[$i]=substr($string,$a,$b);
$wrapped= implode($indent,$l);
return $wrapped;
}

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #4
On Mon, 29 Mar 2004 09:09:15 -0800, lawrence wrote:
but that would include white space. How do you say "everything but
white space" in regex? I realize I'm supposed to use "/\S/" but I
don't understand how. Is this correct:

$pattern = "/.{80}/\S/";
$replaceWith = $pattern." ";


So, taking what you already understand, let's extend that to make it work
for what you want.

"Dot" (.) means "anything"

{80} means "80 of the atom just to the left of the first curly brace"

\S means "any non-whitespace character"

You want an expression that says, "take 80 of any character, followed by
one or more any-characters-that-aren't-whitespace until the next
whitespace, and replace that last whitespace witha newline"

(I realize it is a bit unwieldy to say it like that, but sometimes the
only way I can figure out a regex is by saying it out loud or writing it
down, in English, like I just did.)

So, here goes:

1) Take 80 of any character

/.{80}/

2) Followed by one or more any-characters-that-aren't-whitespace
until the next whitespace

/.{80}\S+\s/

(It is important to say "one or more" here because asterisk (*) means
"zero or more" -- you must use /\S\S*/ or /\S+/ -- (+) means "one or more")

Also, remember, \S is "non-whitespace" and \s is "whitespace".

3) SAVE the part we want! Use parentheses.

/(.{80}\S+)\s/

4) Final regex replace (assuming "$line" is the variable holding the text
you want to wrap):

$line = preg_replace("/(.{80}\S+)\s/", "$1<br>\n", $line);

later...

--
Jeffrey D. Silverman | jeffrey AT jhu DOT edu
Website | http://www.wse.jhu.edu/newtnotes/

Jul 17 '05 #5
Jeffrey, thanks for advice. You take me closer to what I want. I think
I might be able to figure out the rest. However, you mistate the
problem I'm trying to solve. Perhaps I stated it poorly the first
time. You put it like this:
You want an expression that says, "take 80 of any character, followed by
one or more any-characters-that-aren't-whitespace until the next
whitespace, and replace that last whitespace witha newline"
I'd put it like this:

"Look for strings of non-white space characters that are longer than
80. If longer than 80, then insert a white space at the 81st spot."

What I'm worried about is long urls (which tend to wreck the
formatting of a page, especially in Microsoft IE). I'm not trying to
replace whitespaces with newlines, I'm trying to insert white spaces
into long character strings.

Thanks for the help.

Jeffrey Silverman <je*****@jhu.edu> wrote in message news:<pa****************************@jhu.edu>... On Mon, 29 Mar 2004 09:09:15 -0800, lawrence wrote:
but that would include white space. How do you say "everything but
white space" in regex? I realize I'm supposed to use "/\S/" but I
don't understand how. Is this correct:

$pattern = "/.{80}/\S/";
$replaceWith = $pattern." ";


So, taking what you already understand, let's extend that to make it work
for what you want.

"Dot" (.) means "anything"

{80} means "80 of the atom just to the left of the first curly brace"

\S means "any non-whitespace character"

You want an expression that says, "take 80 of any character, followed by
one or more any-characters-that-aren't-whitespace until the next
whitespace, and replace that last whitespace witha newline"

(I realize it is a bit unwieldy to say it like that, but sometimes the
only way I can figure out a regex is by saying it out loud or writing it
down, in English, like I just did.)

So, here goes:

1) Take 80 of any character

/.{80}/

2) Followed by one or more any-characters-that-aren't-whitespace
until the next whitespace

/.{80}\S+\s/

(It is important to say "one or more" here because asterisk (*) means
"zero or more" -- you must use /\S\S*/ or /\S+/ -- (+) means "one or more")

Also, remember, \S is "non-whitespace" and \s is "whitespace".

3) SAVE the part we want! Use parentheses.

/(.{80}\S+)\s/

4) Final regex replace (assuming "$line" is the variable holding the text
you want to wrap):

$line = preg_replace("/(.{80}\S+)\s/", "$1<br>\n", $line);

later...

Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Fabian | last post by:
Hi all there, I have already tried asking for help a couple of days ago. I try to rephrase better my problem: I need to grab a webpage that looks like this: <td width=80 align=center...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
1
by: Sea Sharper | last post by:
Hi, C#, from a FileSystemWatcher I would like to catch all files with a *.* filter but then inside the event handler compare against a list of wildcards (eg '*.abc;*.def') Is there anywhere...
2
by: Ed Brown | last post by:
I'm working on a VB.Net application that needs to do quite a bit of string pattern matching, and am having problems using the "LIKE" operator to match the same string twice in the pattern. For...
5
by: Terry Olsen | last post by:
Is there a good way to find a pattern of bytes/chars in a stream? I've got a serial port connected to a tcp port. I need to be able to catch a unique character string in the stream so that I can...
4
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
8
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
4
by: sherifffruitfly | last post by:
Hi all, I can't see what's wrong with this regex pattern: private int ParsePageViews(string str) { int ret = 0; string pattern = @"Visits.*\n\s*Total\s\.*\s(? <visits>(\d{3})|(\d,\d{3}))";
3
by: =?Utf-8?B?TmF2ZWVu?= | last post by:
Not sure if this is the right forum for this question but couldn'd find another newsgroup. I am new to Regular expressions and would like help in deciding which pattern allows me to split a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...
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...

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.