Jump to content

[SOLVED] Replacing text of a string afterthe last delimiter in the string


jaxdevil

Recommended Posts

I am trying to replace all of the text after the last '_' symbol in a string with the symbols [] , so if it says now some_line_1 or description_10 it will change to some_line_[] and description_[]. The code would need to read the string from left to right, finds the first '_' symbol, deletes everything after that symbol, and appends the symbols [] on the end.

 

Any ideas how to do this?

 

Thanks,

SK

Link to comment
Share on other sites

It would be a combination of substr and strrpos.

 

You would use strpos to locate the last location of the last _ from there you would use substr to pull out 0 to that last position then append the [] to the end of the string.

 

<?php
$string = "index_ok_1";
$newString = substr($string, 0, strrpos($string, '_')) . "[]";

echo $newString;
?>

 

Edit:

Decided to add the code cause I was bored.

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.