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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

wordwrap only on long words help needed

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

0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 4
  • 2
  • 2
  • +1
1 Solution
 
MMDeveloperCommented:
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

0
 
Michael701Commented:
something like this?

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

Open in new window

0
 
ebrandCommented:

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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
St_Aug_Beach_BumAuthor Commented:
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!
0
 
St_Aug_Beach_BumAuthor Commented:
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,

Chris
0
 
St_Aug_Beach_BumAuthor Commented:
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!
0
 
Michael701Commented:
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.
0
 
St_Aug_Beach_BumAuthor Commented:
ah, ok, thank you, I had no idea I didn't need them on all my if statements!
0
 
ebrandCommented:
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now