Take part of a string delimited between characters

I have a string like the one in the example below:

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

My question is, how can I take only the part of the text that is between the bars /, and at the same time take that part out of the main string?

Would have to be like:

$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 one part of my string, and I would need to take all those parts separately and pull out of string main;

Example:

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

Expected output:

$string = 'Lorem ipsum, consectetur.'
$retirado = 'dolor sit amet adipiscing elit';
Author: Fleuquer Lima, 2016-07-19

5 answers

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

To "paste":

This is to only get the data between "/", in this way you will be able to get "what you have" between the "/". You will also get to use them to replace. Since your post says you need "$string "and also the "retir removed", this would be a better 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 variable resultado result:

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;
}

Will 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, in this way 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, as the author demands the " withdrawn$"! For other cases, when there is only a need to replace, without obtaining any data, you 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 one of this function, so I must at least explain 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)

This way the REGEX executes something like:

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

 3
Author: Inkeliz, 2016-07-19 15:42:02

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

One of the simplest ways would be by using 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.

In addition you can use the functions:
preg_split
or
preg_replace

In this case you would use regular expressions

I hope I helped.

Update

For present separately you can do like this:

foreach ($pices as $key => $value) {
    echo "<p><strong>Pedaço $key: </strong>$value</p>";
}
 2
Author: Ricardo Mota, 2016-07-19 14:53:36

You can use preg_replace, as shown below:

preg_replace("/\/(.*\//", "", $string);
 1
Author: Daniel Reis, 2016-07-19 14:18:39

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

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

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

 1
Author: João, 2016-07-19 14:20:02

Try doing like 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

 1
Author: Ivan Ferrer, 2016-07-19 16:47:02