Separate identifier information into variables (PHP)

0

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

    
asked by anonymous 13.03.2015 / 19:53

3 answers

2

Use a regular expression to match only the 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 to match / match only numbers at least once.

Output:

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

)

phpfiddle - example

    
13.03.2015 / 20:33
0

This method below pars the string by breaking it into array using the explode explode () then search for the elements that have the string you are looking for using strpos () and pro inserts into an array only 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 usso will be:

Array
(
    [0] => 123456
    [1] => 124214
)
    
13.03.2015 / 20:37
0

Share 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 the letters or other characters that are not numero

    
13.03.2015 / 22:00