Get part of a delimited string between characters

0

I have string as the example below:

$string = 'Lorem ipsum dolor sit amet, consectetur /adipiscing elit/.';

My question is, how can I get only the part of the text that is between the% s and% s and at the same time remove that part of the% s?

It would have to be type:

$string = 'Lorem ipsum dolor sit amet, consectetur.';
$retirado = 'adipiscing elit';

I used this text as an example, but I can have this markup with / on more than a part of my string , and would need to get all these parts separately and remove from //

Example:

$string = 'Lorem ipsum /dolor sit amet/, consectetur /adipiscing elit/.';

Expected output:

$string = 'Lorem ipsum, consectetur.'
$retirado = 'dolor sit amet adipiscing elit';
    
asked by anonymous 19.07.2016 / 15:58

5 answers

3

You should use preg_match_all when there are multiple values, see your documentation here!

To "catch":

  

This is to ONLY get the data between "/", that way you will be able to get "whatever you have" between the "/". You will also get to use them to replace. Since your posting says you need "$ string" and also "$ removed", that would be the best solution.

// Sua string:
$string = 'Lorem ipsum /dolor sit amet/, consectetur /adipiscing elit/';

// Regex (leia o final para entender!):
$regrex = '/\/(.*?)\//';

// Usa o REGEX:
preg_match_all($regrex, $string, $resultado);

You will get exactly, in the $ result variable:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(16) "/dolor sit amet/"
    [1]=>
    string(17) "/adipiscing elit/"
  }
  [1]=>
  array(2) {
    [0]=>
    string(14) "dolor sit amet"
    [1]=>
    string(15) "adipiscing elit"
  }
}

So you can do one:

foreach($resultado[1] as $texto){
  echo $texto;
}

You'll get:

dolor sit amet
adipiscing elit

To remove:

Using the data already obtained with preg_match_all:

  

This is useful if you need to get the data using preg_match_all , so it will only replace what you already have!

$string = 'Lorem ipsum /dolor sit amet/, consectetur /adipiscing elit/.';

$resultado = str_replace($resultado[0], "", $string);

echo $resultado;

// Retorna:
Lorem ipsum , consectetur .

Using preg_replace:

  

This solution does not fully answer the question since the author requires the "$ withdrawn"!      For other cases, when there is only need to replace, without obtaining any data, can use such a method.

$string = 'Lorem ipsum /dolor sit amet/, consectetur /adipiscing elit/.';

$resultado = preg_replace('/\/(.*?)\//', "" , $string);

echo $resultado;

// retorna:
Lorem ipsum , consectetur .

About REGEX:

Regex is the main function of this function, so I should at least explain it minimally.

/      Inicio do Regex!
\/     Escapa o "/" ("encontre a "/")
(.*?)  Obtenha qualquer caractere
\/     Escapa o "/" ("encontre a "/")
/      Fim do Regex (como estamos com o preg_match_all seria o mesmo de /g)

In this way REGEX performs something like:

Find the "/", get anything until you find the next "/", thus getting everything that is between the "/".

    
19.07.2016 / 17:01
2

In this case, you can use one of the split functions of PHP

One of the simplest ways would be to use explode() which would look like this:

$string = 'Lorem ipsum dolor sit amet, consectetur /adipiscing elit/.';
$pices = explode("/", $string);
//Array ( [0] => Lorem ipsum dolor sit amet, consectetur [1] => adipiscing elit [2] => . )

In this case the function explode() generates an array with the string.

Additionally you can use the functions:
preg_split
or preg_replace

In this case you would use regular expressions

I hope I have helped.

update

To present separately you can do this:

foreach ($pices as $key => $value) {
    echo "<p><strong>Pedaço $key: </strong>$value</p>";
}
    
19.07.2016 / 16:29
1

You can use preg_replace, as shown below:

preg_replace("/\/(.*\//", "", $string);
    
19.07.2016 / 16:18
1

function split_me ($ string, $ start, $ end) {

$ str2 = substr ($ string, strips ($ string, $ start)), strlen ($ start)); $ b = stripos ($ str2, $ end); return trim (substr ($ str2, 0, $ b)); }

$ string = 'Lorem ipsum dolor sit amet, consectetur / adipiscing elit /.'; echo split_me ($ string, '/', '/');

    
19.07.2016 / 16:20
1

Try this:

$string = 'Lorem ipsum /dolor sit amet/, consectetur /adipiscing elit/.';

function removeParseContentBar($string)
{
    $arr = str_split($string);
    $i = 0;
    foreach ($arr as $k => $char) {
        if ($char == '/') {
          /* abre a tag na primeira barra e
             fecha o elemento em tag quando 
             achar a segunda barra */
          $arr[$k] = ($i % 2 == 0) ? '<' : '/>';
        } else {
          $arr[$k] = $char;
          $i++;
        }
        $i++;
    } 
    $content = implode('', $arr);
    //remove a tag
    return strip_tags($content); 
}
echo removeParseContentBar($string);

See working here

    
19.07.2016 / 16:52