0

I have an input string. Format is below:

This;is;my;long;string

Any fast solution (a lot of strings) to get this output string using PHP:

This;is;my;long

I need to remove this appendix: ;string

  • I don't know the length of appendix: ;any letters here.

Thanks!

1

7 Answers 7

5

If you're consistently using ; as the delimiter, you can explode() the string, remove the last index, then rejoin the string using implode():

$str    = "This;is;my;long;string";
$strArr = explode(";", $str);

unset($strArr[count($strArr) - 1]);
$newStr = implode(";", $strArr);

UPDATE

In order to make this work for any searchable string, you can use array_keys():

$str             = "This;is;my;long;string";
$strArr          = explode(";", $str);
$searchStr       = "string";
$caseSensitive   = false;
$stringLocations = array_keys($strArr, $searchStr, $caseSensitive);

foreach ($stringLocations as $key) {
    unset($strArr[$key]);
}
$newStr = implode(";", $strArr);

Or, even quicker, you can use array_diff():

$str       = "This;is;my;long;string";
$strArr    = explode(";", $str);
$searchStr = array("string");
$newArray  = array_diff($searchStr, $strArr);
$newStr    = implode(";", $newArray);
Sign up to request clarification or add additional context in comments.

2 Comments

@Fluffeh I realized that was going to come up, so I updated the answer.
don't use implode/explode since it will be too slow.
4
$str = substr($string, 0, strrpos($string, ';'));

Comments

2

str_replace

<?php 
$string='This;is;my;long;string';
str_replace(';string','',$string); ?>

2 Comments

This is a bit short-sighted. It will only work for this case.
You can adjust the first parameter of str_replace with whatever you need to replace, using a string or array of strings. The question doesn't state the end case, is it always the last element, is it always delimited by ';' - he wanted ;string removing, this does it and can be easily adapted to most other situations.
2
$str = rtrim($str, ';string');

Comments

1

If you know that ;string will always be there (most of the time anyhow) you can use rtrim:

$trimmed = rtrim($text, ";string");

This will remove only the trailing ;string from the string. Unless it exists on the end of the string , it won't be deleted.

Comments

1

Not sure if the piece you want to remove is consistently at the beginning or not.

$array = explode(";" , $string);
foreach ( $array as $key => $arr ) {
    if ( $arr == "string" ) {
        unset($array[$key]);
        break;
    }
    echo implode(";" , $array);
}

Comments

0

If the string is always formatted that way you can't go wrong with substr - probably the fastest way

$str = 'This;is;my;long;string';
$str = substr( $str, 0, -7);

2 Comments

This is a bit short-sighted. It will only work for this case.
i know, but maybe that's what he wanted. he mentioned speed, and substr is faster than str_replace

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.