Jump to content

php question


Miko

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.