Jump to content

How can I edit the same string twice and only get one output string?


Stuart_Westgate

Recommended Posts

This is an example of my code:

 

$status = $entry->title;

 

echo substr(strstr($status," "), 1) . substr($status, 0, -18);

 

 

Below is the outcome:

 

I'm feeling cold in this room. #emotionalclothingPants: I'm feeling cold in this room.

 

 

I want the oucome to look like this:

 

I'm feeling cold in this room.

 

Thus removing the "#emotionalclothing" part of the string

 

How can this be achieved?

 

Thanks Stuart

Link to comment
Share on other sites

You have to store the result of the first operation in the variable, before you attempt the second operation.

What you're doing right now is:

  1. First you remove everything before the first space
    strstr($status," ")


  2. Then you cut that down to the first character only (the space).
    substr (...., 1)


  3. Next you cut the last 18 characters off the original string.
    substr ($status, 0, -18);


  4. Before you concatenate (glue) the two results together, and echo them out.
    echo ***** . *****;

 

Also, from what I can see, you only need to edit the string once, using one function. As Barand just posted. No need to make it more complicated than it have to be, after all.

Edited by Christian F.
Link to comment
Share on other sites

Thanks for your answers guys. It's appreciated.

I've been programming for a few years not and in my final year of uni (still have a lot to learn though lol) but only just started doing PHP.

 

I got a little caught up in the code and just didn't think to simplify it lol.

 

This is how the string would be formated everytime before it's parsed.

So you have item of clothing: Pants:

Emotion: I'm feeling cold in this room.

Hashtag: #emotionalclothing

Pants: I'm feeling cold in this room. #emotionalclothing

 

I done it this way in the end:

$status = $entry->title;

 

$status1 = substr(strstr($status," "), 1);

$status2 = substr($status1, 0, -18);

 

echo $status2;

 

Which outputs:

I'm feeling cold in this room.

 

:happy-04: It's always good to give something back!

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.