We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


wordwrap only on long words help needed

Medium Priority
Last Modified: 2012-08-14
I'm having some trouble with this, a couple of solutions have worked in testing, but in practice in the script not been quite right, but I am learning and narrowing it down :)

I need to use the php wordwrap function, but not on the entire string (which is a post/comment in a forum).  Occasionally someone will put a long url in the post and it messes up the page layout.  I want to break up just that long word/url, while letting the rest of the text flow naturally.

What I think I need here, is something like this:

$string_array=explode(" ",$string);

now some kind of for or while statement that says for any $string_array piece over 70 characters, do this:

$cuttext = wordwrap($longtext, 70, "<br>", true);

then implode it back together with the cuttext back in place?.

I have no idea how to actually write that though.  Thanks for any help,  Chris

Watch Question

try this
$string_array = explode(" ",$string);
$string_array = array_map("wrapLongStrings", $string_array);
function wrapLongStrings($val) {
	if (strlen($val) >= 70) {
		$val = wordwrap($val, 70, "<br />", true);
	} else {}
	return $val;

Open in new window

something like this?

(untested code)
$string_array=explode(" ",$string);
foreach ($string_array as $longtext)
  $final_string.=' '.wordwrap($longtext, 70, "<br>", true);

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


try this:
$longtext = "An example on a long word is: http://www.experts-exchange.com/experts.jsp?etIndex=1&savedSearchID=26992&ssrStart=11&ssrOrderBy=1&ssrSort=-1#ssResults";
echo wordwrap($longtext,75,"<br />\n",TRUE);

Open in new window


Thank you very much, this will do the trick.

Does the foreach need brackets around what it is foreaching? Testing so far seems to indicate that it doesn't, but I just wanted to check.

Thanks again!


Thank you all for your comments/answers.

For what I am doing, Michael701's solution seems to work the best.  I appreciate it all though,



oh, ok, I was trying to figure out why the foreach in Michael701's solution doesn't need brackets.  Did a little research and see that the foreach just works on the array and then stops itself (if that's the right terminology), so I understand.  Working perfectly in the script I have, thanks!
brackets are only requied if you wish to execute more than one statement, same as while and for loops and also if else statements. the brackets only group multiple statements.


ah, ok, thank you, I had no idea I didn't need them on all my if statements!

You don't, but be careful because I know a lot of people who have gotten into trouble because they don't realize they didn't use brackets and added code after the original code was written. This lead them to many hours of confusion about why their code didn't run properly.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.