#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    PHP Function utf8_wordwrap with regex


    Hallo friends! Please help me!
    This function breaks lines till the specified word-length ($width) only by whitespace, but not by hyphen. If there are too many symbols, the string "123456789-987654321" could be broken for example so:
    "123456789-987654
    321",
    but it should be so:
    "123456789-
    987654321".
    How must the function be modified that the lines will be broken both by whitespace and by hyphen?

    function utf8_wordwrap($string, $width, $break, $cut)
    {
    if($cut) {
    // Match anything 1 to $width chars long followed by whitespace or EOS,
    // otherwise match anything $width chars long
    $search = '/(.{1,'.$width.'})(?:\s|$)|(.{'.$width.'})/uS';
    $replace = '$1$2'.$break;
    } else {
    // Anchor the beginning of the pattern with a lookahead
    // to avoid crazy backtracking when words are longer than $width
    $pattern = '/(?=\s)(.{1,'.$width.'})(?:\s|$)/uS';
    $replace = '$1'.$break;
    }
    return preg_replace($search, $replace, $string);
    }

    Many thanks in advance!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Is it right?


    (?:\s|(-)|$) instead of (?:\s|$)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Hannover
    (?:\s|(-)|$) instead of (?:\s|$)
    No, it's not right!

    Result:

    "123456789-
    987654321".

    If it hyphenates, it seems to be OK, but...

    if it doesn't hyphenate:

    "123456789- 987654321".
    Unwanted whitespace!

    Help please!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0

    Way out


    Originally Posted by Hannover
    No, it's not right!

    Result:

    "123456789-
    987654321".

    If it hyphenates, it seems to be OK, but...

    if it doesn't hyphenate:

    "123456789- 987654321".
    Unwanted whitespace!

    Help please!
    I've found a way out of the problem by PHP together with (?:\s|(-)|$) instead of (?:\s|$) thereby:

    utf8_wordwrap($myrow["title"], 30, "\n", true)

    function utf8_wordwrap($string, $width, $break, $cut)
    {
    if($cut) {
    // Match anything 1 to $width chars long followed by whitespace or EOS,
    // otherwise match anything $width chars long
    $search = '/(.{1,'.$width.'})(?:\s|(-)|$)|(.{'.$width.'})/uS';
    $replace = '$1$2'.$break;
    } else {
    // Anchor the beginning of the pattern with a lookahead
    // to avoid crazy backtracking when words are longer than $width
    $pattern = '/(?=\s)(.{1,'.$width.'})(?:\s|(-)|$)/uS';
    $replace = '$1'.$break;
    }
    $output = str_replace("-".$break, "-", preg_replace($search, $replace, $string));
    return $output;
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Hannover
    I've found a way out of the problem by PHP together with (?:\s|(-)|$) instead of (?:\s|$) thereby:

    utf8_wordwrap($myrow["title"], 30, "\n", true)

    function utf8_wordwrap($string, $width, $break, $cut)
    {
    if($cut) {
    // Match anything 1 to $width chars long followed by whitespace or EOS,
    // otherwise match anything $width chars long
    $search = '/(.{1,'.$width.'})(?:\s|(-)|$)|(.{'.$width.'})/uS';
    $replace = '$1$2'.$break;
    } else {
    // Anchor the beginning of the pattern with a lookahead
    // to avoid crazy backtracking when words are longer than $width
    $pattern = '/(?=\s)(.{1,'.$width.'})(?:\s|(-)|$)/uS';
    $replace = '$1'.$break;
    }
    $output = str_replace("-".$break, "-", preg_replace($search, $replace, $string));
    return $output;
    }
    No, it's not OK with (?:\s|(-)|$)! Can no one help???
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    are you talking to yourself, or is this a question for us?

    What makes you think you need regexes? This is a simple string operation: You fetch the n-th character. If it's a space or a dash, you replace it with the break character. That's a one-liner.

    // OK, I see you've stolen that stuff from the php.net comments. That's not a good idea. A lot of the code those people contribute is gargabe. Write your own code.
    Last edited by Jacques1; May 14th, 2013 at 05:07 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    are you talking to yourself, or is this a question for us?

    What makes you think you need regexes? This is a simple string operation: You fetch the n-th character. If it's a space or a dash, you replace it with the break character. That's a one-liner.

    // OK, I see you've stolen that stuff from the php.net comments. That's not a good idea. A lot of the code those people contribute is gargabe. Write your own code.
    Many thanks for Your answer, Jacques! I talk to You in despair. I'm new in regex. I try hardly to understand it, but I don't yet. This code is a part of many tutorials for me. It works well, but not completely. Is it possible to modify $search = '/(.{1,'.$width.'})(?:\s|$)|(.{'.$width.'})/uS'; so that the code fetches the n-th character and if it's a space, the code replaces it with the break character, but if it's a dash, the code should use it for a line break without replacing the dash?? I have unfortunately no idea and find no thought-provoking impulse. Sorry! Verzeihung!

IMN logo majestic logo threadwatch logo seochat tools logo