473,626 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

$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 processStringFo rPublic($string , $return=0,
$class="mcParag raph") {
// 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($stringAr ray); $i++) {
$name = makeDisplayText FromString($str ingArray[$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 = processHtmlTagE ndings($string, "<i>");
$string = processHtmlTagE ndings($string, "<b>");
$string = processHtmlTagE ndings($string, "<u>");
$string = processHtmlTagE ndings($string, "<em>");
$string = processHtmlTagE ndings($string, "<strong>") ;
$string = processHtmlTagE ndings($string, "<center>") ;
$string = processHtmlTagE ndings($string, "<a ");
$string = processHtmlTagE ndings($string, "<div ");
$string = processHtmlTagE ndings($string, "<span ");
$string = processHtmlTagE ndings($string, "<p ");
$string = processHtmlTagE ndings($string, "<ul ");
$string = processHtmlTagE ndings($string, "<div ");
$string = processHtmlTagE ndings($string, "<span ");
$string = processHtmlTagE ndings($string, "<ol ");
$string = processHtmlTagE ndings($string, "<span ");
$string = processHtmlTagE ndings($string, "<p ");
$string = processHtmlTagE ndings($string, "<p>");
$string = processHtmlTagE ndings($string, "<div>");
$string = processHtmlTagE ndings($string, "<span>");
$string = processHtmlTagE ndings($string, "<ul>");
$string = processHtmlTagE ndings($string, "<div>");
$string = processHtmlTagE ndings($string, "<span>");
$string = processHtmlTagE ndings($string, "<ol>");

if ($return) {
return $string;
} else { echo $string;
}
}
Jul 17 '05 #1
5 2693
With total disregard for any kind of safety measures
lk******@geocit ies.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*****@HOLYfl atnetSHIT.net> wrote in message news:<Xn******* *************** ***@216.196.97. 132>...
With total disregard for any kind of safety measures
lk******@geocit ies.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($strin g,$wrap,$indent )
{
$a=0;
$i=0;
while (substr($string ,$a+$b,1)!=""){
$b=$wrap;
while(substr($s tring,$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.ed u> 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
1970
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 valign=top><a href="<link that should not be grabbed by the pattern>" id=r><img src=image.jpg width=66 height=79 alt="" border=1><br><font size=-2>Bla Bla text</font></a></td><td
4
9740
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 numeric value according to an arbitrary regular expression.
1
7019
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 inside the Framework where I can do something like Pattern x = new Pattern("*.abc"); if ( x.Matches(strFilename) )
2
6001
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 example, in the following code: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim theString As String theString = "1234 TEST 5432 TEST ABCD" If theString Like "*TEST*TEST*" Then...
5
1650
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 perform certain functions. For example, I have a telnet client connected to an Apple II through the serial port. The user at the telnet terminal is using the BBS running on the Apple II just like the good ole days of dialup BBS's. I need to be...
4
3602
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 "LOT:". Source text may contain multiple "SUB: ... LOT:" blocks. For example this is my source text:
8
10283
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 bunch of hits with this - I'm only interested in the *last* one. Is there a way to build the concept "last" into the
4
2432
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
4871
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 string into sets of words based on capital letter. For e.g. if i have a string "FirstnameLastname" I would like the result to return me Firstname and Lastname. The other conditions of the input string are 1) If whitespace exists, do not return a...
0
8269
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7203
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.