delete words in php when finding a -

3

I have the following data list of a WHILE in PHP

<?php
      $query   = "SELECT * FROM table where value='$thisvalue'";
      $result  = mysqli_query($conexion, $query);
      while ($row = mysqli_fetch_assoc($result)) {
       $campo = $row['value'];
       //QUITAR PALABRAS SI ENCUENTRA UN - SOLO PALABRAS.
       echo $campo;
       }


    PANELEGP00001
    PANELEGP00003
    PANELEGP00001-1
    PANELEGP00002-TOS
    PANELEGP00004-2
    LIVOR-44_900_2100

I wish I could remove the words ONLY THE WORDS when I found a - . I've been looking for methods in PHP try with str_replace but I have not had results.

I would like a result like that.

    PANELEGP00001
    PANELEGP00003
    PANELEGP00001-1
    PANELEGP00002   //SE QUITO LA PALABRA TOS
    PANELEGP00004-2
    LIVOR-44_900_2100
    
asked by MoteCL 11.10.2018 в 19:32
source

2 answers

3

You can use explode and is_numeric like this:

<?php

    $valores = array("PANELEGP00001",
    "PANELEGP00003",
    "PANELEGP00001-1",
    "PANELEGP00002-TOS",
    "PANELEGP00004-2",
    "LIVOR-44_900_2100"
  );

  foreach ($valores as $i => &$valor) {
      $palabras = explode("-", $valor);      
      if (isset($palabras[1]) && preg_match("/[a-z]/i", $palabras[1])) {          
          $valor = $palabras[0];
      }
  }
  var_dump($valores);

?>
    
answered by 11.10.2018 / 19:42
source
0

I think that for this it is best to use preg_replace ( link ) with a regular expression. For example, to look for the text that you decide, in the line that says

//QUITAR PALABRAS SI ENCUENTRA UN - SOLO PALABRAS.

you should put

$campo = preg_replace('\-\D+', '', $campo);

Good luck!

    
answered by 11.10.2018 в 20:13