mikka23 Posted January 2, 2010 Share Posted January 2, 2010 Hi, I am not the greatest at PHP (no actual knowlege just look up when I have a problem). I searched for this problem on google but didn't return anything which seemed efficient. What I am trying to do is split a sentence into two or insert a </span> in the middle. Its for a wordpress theme and I want the title of post to be like the following so the first half can be made a different colour: <span>This is the</span> title splt made from a string "This is the title split". Does anyone know how to do this. Help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/ Share on other sites More sharing options...
mikesta707 Posted January 2, 2010 Share Posted January 2, 2010 do you know exactly what the title is? or will it change? Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/#findComment-987354 Share on other sites More sharing options...
mapleleaf Posted January 2, 2010 Share Posted January 2, 2010 You could use: $num = strlen($title) ; $num = $num/2; $first_half = substr($title,0, $num,); $second_half = substr($title, $num); Something like that. Of course you might end up with a word changing colour midway. Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/#findComment-987355 Share on other sites More sharing options...
MadTechie Posted January 2, 2010 Share Posted January 2, 2010 Their are many ways to split the string, most will create an array then you just concatenate them back together, but we need to know how you wish the split it up, for example to split it in half you could do this <?php $str = "This is the title split"; $words = str_word_count($str, 1); $first_half = ceil(count($words)/2); echo "<span >"; for($n=0;$n<$first_half;$n++) echo $words[$n]." "; echo "</span>"; for($n=$first_half;$n<count($words);$n++) echo $words[$n]." "; ?> or using word wrap <?php $str = "This is the title split"; $str = str_replace("\n","",$str); //remove linespace $text = explode("\n",wordwrap($str, 20, "\n")); //word wrap at 20 char & put in array foreach($text as $newtext){ echo "<span>$newtext</span>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/#findComment-987357 Share on other sites More sharing options...
mikka23 Posted January 2, 2010 Author Share Posted January 2, 2010 Thanks for the replys guys. Im trying them out now. I think I would prefer to not split a word in half but if its a lot more difficult then I suppose it will do. I tried out the first suggested code but I am getting an error on the following line (extra comma?) $first_half = substr($title,0, $num,); Thanks for the help guys, ive honestly never experienced such quick and helpful posts. Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/#findComment-987359 Share on other sites More sharing options...
MadTechie Posted January 2, 2010 Share Posted January 2, 2010 Thanks for the help guys, ive honestly never experienced such quick and helpful posts. That's because your new here, At PHPFreaks if you don't get a quick response then you probably asked the wrong question Oh and on that note welcome EDIT: oh an my example don't split words Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/#findComment-987361 Share on other sites More sharing options...
mikka23 Posted January 2, 2010 Author Share Posted January 2, 2010 I just tried your first example MadTechie, it seems just what I was looking for. The only problem is getting the variable from wordpress (I though it was very simple). <?php $str = single_cat_title(); $words = str_word_count($str, 1); $first_half = ceil(count($words)/2); echo "<h2><span>"; for($n=0;$n<$first_half;$n++) echo $words[$n]." "; echo "</span>"; for($n=$first_half;$n<count($words);$n++) echo $words[$n]." "; echo "</h2>"; ?> Now the problem is getting the category title into a variable (currently a function). Im assuming Im doing something extremely stupid. $str = single_cat_title(); Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/#findComment-987362 Share on other sites More sharing options...
mikka23 Posted January 3, 2010 Author Share Posted January 3, 2010 Sorry for the above post. I worked it out. For anyone wondering its just add the following: single_cat_title("", false); Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/#findComment-987365 Share on other sites More sharing options...
MadTechie Posted January 3, 2010 Share Posted January 3, 2010 Cool If this is solved please click the topic solved button (Bottom left) (this saves other members wasting time reading posts that have already been solved) you can unsolved it by clicking the button again (it gets toggled to unsolved) Quote Link to comment https://forums.phpfreaks.com/topic/186978-split-a-string-in-half-or-insert-into-middle/#findComment-987369 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.