Separate identifier information into variables (PHP)

Asked

Viewed 217 times

0

Ex: text text id:123456 text text id:124214 text text I need to separate id values into variables as I can do this in PHP?

  • Do you want to pick only the numbers? You can give an example of the expected output

  • want only the numbers, why I will replace the id in the string: by a link using the number as parameter for the link, instead of id:123456 will be <a href="url/123456">123456</a>

3 answers

2

Use a regular expression to combine only numbers, the appropriate function for this is preg_match_all

<?php
$str = 'texto texto text id:123456 text text id:124214 text text';
$er = '/\d+/';
preg_match_all($er, $str, $ocorrencias);

echo '<pre>';
print_r($ocorrencias);

\d+ means matching/marrying only numbers at least once.

Exit:

Array
(
    [0] => Array
        (
            [0] => 123456
            [1] => 124214
        )

)

phpfiddle - example

  • Further where the identifier used in that function in case the ID:

  • @Rdallagnol Not just numbers you needed? what was missing?

  • for example in string will come with an identifier before and may have several identifiers for example id:,ch:, bc: (text bc:1111, text text id:4444)

  • You always have to pick two characters, the two dots and the number. ex: [a-z]:([0-9]+), change the value of $er for $er = '/([a-z]+:)(\d+)/'; See if that’s what you expect, @Rdallagnol

  • @rray I would put the "id:" in regex, something like $er = '/id: d+/';

0

Guys with your help

 $parser = explode(" ", $atualizacao);

     /**** CH ******************/
     foreach ($parser as $data) {
         if (strpos($data, "ch#") !== false) {
             $dataId = explode("ch#", $data);
             $ids[] = $dataId[1];
         }
     }
     //var_dump($ids);
     foreach ($ids as $i => $ch) {
        preg_replace("/\D/", "", $ch);
        $atualizacao = str_replace('ch#' . $ch, '<a href="LINK/' . > $ch . '">' . $ch . '</a>', $atualizacao);
     }

now the only thing missing is to remove letters or other characters that are not number

0

This method below makes the parse in the string breaking it into array using the explode explode() then searches for which elements have the string you are looking for using the strpos() and then inserts it into a array just the values you need.

$string = "texto texto text id:123456 text text id:124214 text text";
$parser = explode(" ", $string);

foreach($parser as $data){
    if(strpos($data, "id:") !== false) {
        $dataId = explode("id:", $data);
        $ids[] = $dataId[1];
    }
}
echo "<pre>";
print_r($ids);

The return to us will be:

Array
(
    [0] => 123456
    [1] => 124214
)
  • That function didn’t work Erlon

  • What was the mistake?

  • It did not return anything inside the array, I will paste the result Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => )

  • This code didn’t exactly work or the mlocal you applied didn’t work?

  • I simply copied and pasted this code and showed the result I posted earlier

  • fault my forgot to do the denial of identical. I edited

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.