PHP String Split

Hi Guys,

I have a string in php of variable lenght,

I want to to split the words up at a max of say 5 chars to keep it simple,



For example

hi there

this would split at the of "hi" as "there" would push it over the max char.


hello there would split at hello as hello is 5 chars

hi I am John

would split at  "hi I " hi = 2  i = 1 two spaces = 2 total 5

Hope this makes sense

here is my attempt


<?php 
				
				$strToSplit = get_the_title();
	
				$pieces = str_split($strToSplit);
				
				
				$maxLen = 12;
				
				$stringOut = array();
				
				$currentOut = "";
				
				foreach($pieces as $piece)
				{
					$currentLen = strlen($piece);
					
					if(($currentOut +$currentLen)  <= $maxLen)
					{
						$currentOut= $currentOut." " .$piece;
					}
					else
					{	
						echo("Push");
						array_push($stringOut,$currentOut);
						$currentOut = "";
					}
				}
				
				$outputbuffer ="";
				
				foreach($stringOut as $stringOuttxt)
				{
					$outputbuffer =  $outputbuffer . "<span>".$stringOuttxt."<span>";
				
				}
				
				echo($outputbuffer);
				
				?>
			

Open in new window





john_yourspaceAsked:
Who is Participating?
 
Ray PaseurCommented:
http://www.laprbass.com/RAY_temp_john.php

Outputs:
One Pot Roast
Pork Chops
with Creamy
Mash
<?php // RAY_temp_john.php
error_reporting(E_ALL);

// TEST DATA
$str = "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
 
haloexpertsexchangeCommented:
you should be able to just use str_split()
http://php.net/manual/en/function.str-split.php
str_split can take a second parameter which specifies the length of each section that is split.
so you should just be able to do this
				$strToSplit = get_the_title();
	
				$pieces = str_split($strToSplit,5);

Open in new window

0
 
haloexpertsexchangeCommented:
on a rereading of your question I realize that my answer is probably not what you are looking for.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
haloexpertsexchangeCommented:
what happens if a word in and of itself is longer than the 5 character limit?
An easy way to get each individual word, provided that they are all separated by the same thing, is to use the explode function.
0
 
käµfm³d 👽Commented:
How about this?

<?php

	$input = 'hi there';

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

	var_dump($result);
?>

Open in new window

0
 
john_yourspaceAuthor Commented:
Hi guys thanks for the reply,

A little more insight on what i am at may help

I have a line on my page which can fit a max of 13 chars wide before needing to wrap

take the folowing example

"One Pot Roast Pork Chops with Creamy Mash"

i need to go in 13 chars and split this in to a second line, but if i am mid work on the 13th char go back to the space before hand

in this example

One Pot Roast
Pork Chops wi
th Creamy Ma
sh

this is how it would look how ever i need it to split like this

One Pot Roast
Pork Chops
with Creamy
Mash

so no line is longer then 13 and it still reads perfectly.

The reason for is i plan on using this with some css styling and this the only way to do it


John




0
 
käµfm³d 👽Commented:
You can modify mine to this:

<?php

	$input = 'One Pot Roast Pork Chops with Creamy Mash';

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

	var_dump($result);

?>

Open in new window


You'll have some leading spaces (and possibly trailing spaces) on some entries, so may want to loop through the matches and trim them.


Untitled.png
0
 
käµfm³d 👽Commented:
*argh*  Beat out by the API!!  Curse you documentation!!!!

Good show Ray  = )
0
 
john_yourspaceAuthor Commented:
I used the regexp thanks guys
0
 
Ray PaseurCommented:
@kaufmed ;-)

It's not the first time the wordwrap issue has come up.  I think it goes back to the days of plain-text email messages!
0
 
john_yourspaceAuthor Commented:
Guys how would I update the regexp to include special chars

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

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.