Miko Posted September 25, 2010 Share Posted September 25, 2010 Hello, I have a 'strange' situtation. I have this string "Who Am I" that will be used as an header (site for a client) but the design shows that the word "Who" and the words "Am I" are in 2 different colors, in HTML this is a quick fix, but in PHP? What I was thinking was explode the string and fetch the first element put it in a var en then concat the 2 others. But what if the title is more then 3 elements after exploding? It needs to be more dynamic, but I don't know how to do it. Does anyone have an idea? Quote Link to comment https://forums.phpfreaks.com/topic/214389-php-question/ Share on other sites More sharing options...
PaulRyan Posted September 25, 2010 Share Posted September 25, 2010 You are half way there with your idea, as it will work. What do you want to do if the header is more than 3 words long? Do you want a different color text for the words? Do you want to carry on using the color being used for "Am I" or use the color used for "Who"? If we know what you'd like to do, it would make it a little simpler for us Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/214389-php-question/#findComment-1115634 Share on other sites More sharing options...
Miko Posted September 25, 2010 Author Share Posted September 25, 2010 The design shows me that the headers are only in 2 colors, so if the header has more then 3 words it will still stay the same and have 2 different colors (it's pretty nice though). The main thing that I need is to figger out how to fetch the 'unknown amount' of elements after the first element (after exploding off course). Quote Link to comment https://forums.phpfreaks.com/topic/214389-php-question/#findComment-1115635 Share on other sites More sharing options...
PaulRyan Posted September 25, 2010 Share Posted September 25, 2010 Ohh right I understand, try the following... <?php function splitTitle($title) { // Split title $splitTitle = explode(" ",$title); // Count words in title $countWords = count($splitTitle); // Add each word EXCEPT first to $otherWords variable for($i=1; $i<$countWords; $i++) { $otherWords .= ' '.$splitTitle[$i]; } return array($splitTitle[0], $otherWords); // Return the array of words } $title = splitTitle('Who Am I People Lover'); // Set title as function echo '<span style="color:red;">',$title[0],'</span>'; // Returns first word from title echo '<span style="color:green;">',$title[1],'</span>'; // Returns other words from title ?> Tell me how it goes bud. Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/214389-php-question/#findComment-1115637 Share on other sites More sharing options...
Miko Posted September 25, 2010 Author Share Posted September 25, 2010 hot damn it works!! thanks mate! Quote Link to comment https://forums.phpfreaks.com/topic/214389-php-question/#findComment-1115645 Share on other sites More sharing options...
PaulRyan Posted September 25, 2010 Share Posted September 25, 2010 Glad I could help Miko Quote Link to comment https://forums.phpfreaks.com/topic/214389-php-question/#findComment-1115647 Share on other sites More sharing options...
DavidAM Posted September 26, 2010 Share Posted September 26, 2010 Also, consider the third parameter to explode(). It can be used to specify the number of elements to be generated. $parts = explode(' ', 'Who am I', 2); // $parts[0] will be 'Who' // $parts[1] will be 'am I' Quote Link to comment https://forums.phpfreaks.com/topic/214389-php-question/#findComment-1115742 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.