PHP REGEXP

Guys how would I update the regexp to include special chars

preg_match_all('/[a-z ]{1,13}(?![a-z])/i', $input, $result);

Attached is the context of my code

it is a direct followup to

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/CSS/Q_27402940.html


The problem with this code is whn some enters

John's Recipes

its splits on the '
<?php 
			
				$input = get_the_title();

				preg_match_all('/[a-z ]{1,13}(?![a-z])/i', $input, $result);
				
				$result = $result[0];
				
				foreach($result as $line)
				{
					echo("<span class='titleText'>".ltrim($line)."</span>");
				}
			
			?>

Open in new window

john_yourspaceAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
See if this floats the boat...
http://www.laprbass.com/RAY_temp_john.php
<?php // RAY_temp_john.php
error_reporting(E_ALL);

// TEST DATA
$str = "John's Recipes Include *$%#@&?? One Pot Roast Pork Chops with Creamy Mash";

// MAN PAGE http://php.net/manual/en/function.wordwrap.php
$new = wordwrap($str, 13, '|');

// MAKE AN ARRAY FROM THE INPUT STRING
$arr = explode('|', $new);
foreach ($arr as $key => $val)
{
    // CREATE THE SPAN TAGS AROUND EACH WORD
    $spn
    = '<span THINGIES>'
    . htmlentities($val)
    . '</span>'
    . '<br/>'
    . PHP_EOL
    ;
    $arr[$key] = $spn;
}

// RECONSTRUCT THE STRING
$out = implode(NULL, $arr);


// SHOW THE WORK PRODUCT
echo "<br/>";
echo PHP_EOL . $out;

Open in new window

0
 
Ray PaseurCommented:
update the regexp to include special chars

Just insert the special character(s) to the regex string.  You will want to use preg_quote() if you insert any of the regex metacharacters.
0
 
Ray PaseurCommented:
But seriously, why not just use the built-in PHP function wordwrap() like I showed in the other question.  It worked there and it will work here, too.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
zicevaCommented:
Try replacing the regex from

'/[a-z ]{1,13}(?![a-z])/i'

to

'/[a-z\' ]{1,13}(?![a-z])/i'
0
 
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_john.php

outputs:
John's
Recipes
Include
*$%#@&?? One
Pot Roast
Pork Chops
with Creamy
Mash
<?php // RAY_temp_john.php
error_reporting(E_ALL);

// TEST DATA
$str = "John's Recipes Include *$%#@&?? One Pot Roast Pork Chops with Creamy Mash";

// MAN PAGE http://php.net/manual/en/function.wordwrap.php
$out = wordwrap($str, 13, '<br/>');

// SHOW THE WORK PRODUCT
echo $out;

Open in new window

0
 
john_yourspaceAuthor Commented:
Hey Ray,

The only reason I havent used it is I am wrapping everything in a span

echo("<span class='titleText'>".ltrim($line)."</span>");

and wasn't 100% and I am pushed on time
0
 
john_yourspaceAuthor Commented:
Hey guys

that reg exp does not work on Kevin's Desserts

I have my code now changed to echo wordwrap(get_the_title(),13,"<br />\n");

how do i wrap each word in a span as above?
0
 
Ray PaseurCommented:
The only reason I havent used it is I am wrapping everything in a span...

Good grief! Next time, please tell us about critical "little details" like this.  The question you asked never mentioned a <span> requirement, so naturally you got the exact answer to your question without any consideration of a <span>.  <span> changes the length of the string!  The string length is a critical part of the information you feed to wordwrap().  It's like we knew you wanted a "vehicle," but we didn't know you needed a vehicle that could travel under water.

You can experiment with the URL argument to find the actual length you want to use for the wordwrap() after you have added the tags and, if necessary, entitized the text for safe display in the client browser.  In this example, I found that something in the 39-44 range seemed to be a happy value.  See lines 19-21 where the script visualizes the changes in string length that result from adding the tags and entitization.  See line 28 where the script uses the value in the URL GET argument for the length that is passed into wordwrap().
http://www.laprbass.com/RAY_temp_john.php?ww=42

Best of luck with it, ~Ray
<?php // RAY_temp_john.php?ww=42
error_reporting(E_ALL);

// TEST DATA
$str = "John's Recipes Include *$%#@&?? One Pot Roast Pork Chops with Creamy Mash";

// MAKE AN ARRAY FROM THE INPUT STRING
$arr = explode(' ', $str);
foreach ($arr as $key => $val)
{
    // CREATE THE SPAN TAGS AROUND EACH WORD
    $spn
    = '<span>'
    . htmlentities($val)
    . '</span>'
    ;
    $arr[$key] = $spn;

    // SHOW THE CHANGES IN LENGTH
    $len = strlen($val);
    echo "<br/>$spn HAD STRLEN() = $len AND NOW HAS STRLEN() = " . strlen($spn);
}

// RECONSTRUCT THE STRING
$new = implode(' ', $arr);

// MAN PAGE http://php.net/manual/en/function.wordwrap.php
$out = wordwrap($new, $_GET["ww"], '<br/>');

// SHOW THE WORK PRODUCT
echo "<br/>";
echo PHP_EOL . $out;

Open in new window

0
 
john_yourspaceAuthor Commented:
hey Ray,

Great work, but I only need to wrap each chunk of words in the span

ie
the reg exp looks for 13 chars

John's = 6 + space + "likes" = 12   so span this as it fits in the 13 chars

<span>John's likes </span> <span>cake</span>
0
 
Ray PaseurCommented:
Got it.  Back in a moment with a script.
0
 
john_yourspaceAuthor Commented:
Excellent Ray A++++++++++
0
 
Ray PaseurCommented:
Thanks for the points, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.