0

I have two strings like this:

$str_1 = 'catalog/view/javascript/bootstrap/css/bootstrap.min.css';
$str_2 = 'catalog/view/javascript/font-awesome/css/font-awesome.min.css';

How can I remove 'catalog/view/javascript/' from $str_2.

Thank you!

2
  • you can use explode("/",$str); then you use unset or any remove method Commented Nov 2, 2016 at 7:36
  • I did do it. But I want to know some shorter code like XOR operator to get the similarity string or I remove all string is same in two variable. Commented Nov 3, 2016 at 3:46

2 Answers 2

1

Try this

$str_2 = 'catalog/view/javascript/font-awesome/css/font-awesome.min.css';
echo str_replace( "catalog/view/javascript/", "", $str_2 );
Sign up to request clarification or add additional context in comments.

2 Comments

I think he wants to determine what should be removed from $str_1.
Possible but that is not what he has written in the question. He has asked how to remove part of the string. If the OP wants a comparison then I am sure something else can be written.
0

demo here

<?php
$str_1 = 'catalog/view/javascript/bootstrap/css/bootstrap.min.css';
$str_2 = 'catalog/view/javascript/font-awesome/css/font-awesome.min.css';
$length = strlen($str_1);
$index = 0;
while($index < $length)
{
    if(strpos($str_2, substr($str_1, 0, $index + 1)) === false)
        break;
    $index++;
}
$str_2 =  substr($str_2, $index);
echo $str_2;

3 Comments

It seems interesting but I need some shorter code. Thankyou!
@Diego I thought you were remove the same part with str_1 from str_2. It seems you don't want that, $str_1 misleads me?????. $str_2 = 'catalog/view/javascript/font-awesome/css/font-awesome.min.css'; echo $str_2 = substr($str_2, strlen('catalog/view/javascript/')); works for you.
Oh, I'm so sorry if I make you sad. I mean, your code is working, but I want to some shorter code like XOR operator, maybe! Ex: while($str_1[$i] ^ $str_2[$i]) { $i++} ....

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.