[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP REGEXP

Posted on 2011-10-20
12
Medium Priority
?
190 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:john_yourspace
  • 7
  • 4
12 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37001661
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37001671
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
 
LVL 7

Expert Comment

by:ziceva
ID: 37001695
Try replacing the regex from

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

to

'/[a-z\' ]{1,13}(?![a-z])/i'
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37001720
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
 

Author Comment

by:john_yourspace
ID: 37005313
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
 

Author Comment

by:john_yourspace
ID: 37005386
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37005892
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
 

Author Comment

by:john_yourspace
ID: 37006738
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37006803
Got it.  Back in a moment with a script.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37006853
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
 

Author Closing Comment

by:john_yourspace
ID: 37019205
Excellent Ray A++++++++++
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37023636
Thanks for the points, ~Ray
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
Suggested Courses
Course of the Month18 days, 15 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question