$pattern = "/.{80}/\S/"; - another regex question
From: lawrence (lkrubner_at_geocities.com)
Date: 03/29/04
- Next message: Phil Roberts: "Re: PHP and ASP session"
- Previous message: Savut: "Re: PHP native graphics/image function"
- Next in thread: Phil Roberts: "Re: $pattern = "/.{80}/\S/"; - another regex question"
- Reply: Phil Roberts: "Re: $pattern = "/.{80}/\S/"; - another regex question"
- Reply: Jeffrey Silverman: "Re: $pattern = "/.{80}/\S/"; - another regex question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Mar 2004 09:09:15 -0800
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 is needed so these
paragraphs can be used for spacing. An empty paragraph won't create a
space
$string .= " </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;
}
}
- Next message: Phil Roberts: "Re: PHP and ASP session"
- Previous message: Savut: "Re: PHP native graphics/image function"
- Next in thread: Phil Roberts: "Re: $pattern = "/.{80}/\S/"; - another regex question"
- Reply: Phil Roberts: "Re: $pattern = "/.{80}/\S/"; - another regex question"
- Reply: Jeffrey Silverman: "Re: $pattern = "/.{80}/\S/"; - another regex question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|