0

How can i remove substring from string by entering first and last character of substring in php

For example in string "Hello my name is John" if i enter 'n' and 's' it should return "Hello my John"... Please Help

4
  • 1
    tried strpos over substr? Commented Dec 11, 2014 at 13:49
  • 2
    What if the letters appear more than once in the string? Like Hello my last name is Doe and my first name is John. And what if only one of the two letters appear in the string? Commented Dec 11, 2014 at 13:53
  • 1
    You can use a regulare expression also, but why do you want to do it_ Commented Dec 11, 2014 at 13:55
  • yes using regular expression with preg_replace works fine Commented Dec 11, 2014 at 14:06

3 Answers 3

1
/**
 * Process function.
 * For lack of a better name.
 */
function process($f, $l, $subject)
{
    return preg_replace(sprintf('/%s.*?%s/', preg_quote($f), preg_quote($l)), '', $subject);
}

$f = 'n';
$l = 's';

echo process($f, $l, 'Hello my last name is Doe and my first name is John');

Output:

Hello my last  Doe at  John

I added utility, but it is effectively the same as preg_replace('/n.*?s/', '', $subject).

Sign up to request clarification or add additional context in comments.

4 Comments

it cuts the n of John, poor men :/
copy n paste mistake, fixed output. still the worst function i have ever written though :S
i think i've tomatos on my eyes x.X in your replace, souldn't $n and $s be $f and $l? or i'm unable to read your code :\
yep, again, my lack of commitment to this terrible answer made me typo. fixed that
1

with your string given:

$var = "Hello my name is John";

$sub = getSubString($var,"n","s");

echo $sub;

function getSubString($str,$start,$end) {

    if(($pos1=strpos($str,$start))===false)
        return $str; // not found


    if(($pos2=strpos($str,$end,$pos1))===false)
        return $str; // not found

    return substr($str,0,$pos1).substr($str,$pos2+1);
}

results in:

Hello my John

2 Comments

strpos has an offset parameter that should probably be used (in case second character is present before first ("sHello my name is John" would fail in your example).
Not a problem. Figure you already had a 90% bid in, i'll let you and Marta get the rep points.
1

You should get the position of the first and second letter and use strpos for the method substr

function my_Cut($string, $first_letter, $second_letter){
   $pos[] = strpos($string, $first_letter);
   $pos[] = strpos($string, $second_letter);
   $result = substr($string, $pos[0] , -($pos[1]-$pos[0]-2)); 
   return str_replace ($result, "", $string);
 }

  $string = "Hello my name is John";
  echo my_Cut($string, "n", "s");

Something like this... I think.

2 Comments

Same thing as UnskilledFreak, strpos should be used with $offset parameter to avoid instances where $second_letter occurs before $first_letter.
You are right, thanks for your correction. Anyway I think my answer isn't correct (I can't test it now)

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.