Home > Software design >  Replace substring till space is not found at specified position in PHP
Replace substring till space is not found at specified position in PHP

Time:05-15

I have this main string

$string="This is a sample text";
$substring=substr_replace($string, '', 11 , 6);

Output:

This is a text

I'm passing $string variable to substr_replace function, from 11 index to 6 index i.e. sample will be replaced to blank string but i don't want to pass 6 here I want to replace the characters till the first space is found only i have offset.

Any solution to resolve this issue. Thanks

CodePudding user response:

You can use strpos with offset to find the position of first space at/after the specified position; then use substr_replace to remove the substring:

$tests = [
    "This is a sample text",
    "This is a dummy text",
    "This is a test text"
];
foreach($tests as $test) {
    $start_pos = 10;
    $space_pos = strpos($test, " ", $start_pos);
    assert($space_pos !== false);
    $result = substr_replace($test, "", $start_pos, $space_pos - $start_pos   1);
    echo $test . " -> " . $result . PHP_EOL;
}

# This is a sample text -> This is a text
# This is a dummy text -> This is a text
# This is a test text -> This is a text

CodePudding user response:

function niceCut($str, $cut) {
    $ns = '';
    for ($i = 0; $i < strlen($str); $i  ) {
        $ns .= $str[$i];
        if ($i > $cut && ($str[$i] === " ")) {
            return $ns; 
        } 
        
    }
    return $ns;
}


echo '<p>'. niceCut( "Hello World and hello Text" , 3 ) . '</p>'; // Hello
echo '<p>'. niceCut( "Hello World and hello Text" , 5 ) . '</p>'; // Hello World

CodePudding user response:

I would definitely do this with a regex. I inferred from your question that you want to remove the 4th word in the sentence. Meanwhile if what you want is keep the the first 10 chars and remove anything following until the next space, this is still possible. The following example demonstrate the 2 possibilities:

<?php
$tests[] = "This is a sample text";
$tests[] = "This is a sample text which can get longer";
$tests[] = "A random sentence ausage we can cleanup";

$word_based_regex = '/^((\w* ){3})(\w* )(.*)$/';
$position_based_regex = '/^(.{10})(\w* )(.*)$/';

foreach ($tests as $sentence) {
    echo "original sentence: $sentence" . PHP_EOL;
    echo "replace with word regex: " . preg_replace($word_based_regex, '\1\4', $sentence) . PHP_EOL;
    echo "replace with position regex: " . preg_replace($position_based_regex, '\1\3', $sentence) . PHP_EOL;
    echo PHP_EOL;
}

Which gives:

$ php test_file.php
original sentence: This is a sample text
replace with word regex: This is a text
replace with position regex: This is a text

original sentence: This is a sample text which can get longer
replace with word regex: This is a text which can get longer
replace with position regex: This is a text which can get longer

original sentence: A random sentence ausage we can cleanup
replace with word regex: A random sentence we can cleanup
replace with position regex: A random sausage we can cleanup
  •  Tags:  
  • php
  • Related