Jump to content

Recommended Posts

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.

 

 

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";
}
?>

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.

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  :P

 

Oh and on that note welcome :hail_freaks:

 

EDIT: oh an my example don't split words

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();

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)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.