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

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.