I need to iterate with PHP values that I bring from my Database and that are in brackets [158][547][1268]
, I thought in an explode but I need the value that is inside the brackets. Thanks!
I need to iterate with PHP values that I bring from my Database and that are in brackets [158][547][1268]
, I thought in an explode but I need the value that is inside the brackets. Thanks!
Alternative without regexp
[
]
][
with ,
<?php
$cadena = "[158][547][1268]";
$cadena = str_replace( '][', ',', rtrim( ltrim( $cadena, '['), ']' ) );
echo $cadena;
result:
158,547,1268
$cadena = "[158][547][1268]";
//fuera []
$cadena = preg_replace('/[^A-Za-z0-9\-]/', ' ', $cadena);
//fuera espacios
$cadena=trim($cadena);
echo $cadena;
// explode con doble espacio and voila !!
$porciones = explode(" ", $cadena);
print_r($porciones);
Look this: link
To obtain the numeric data from the string $cadena = "[158][547][1268]"
separated by space you can do it this way:
<?php
$cadena = "[158][547][1268]";
$cadena = preg_replace('/[^A-Za-z0-9\-]/',' ', $cadena);
$cadena=trim($cadena);
$cadena = str_replace(' ', ' ', $cadena);
print_r($cadena);
to get:
158 547 1268
If you want to separate them by comma you would do it this way:
<?php
$cadena = "[158][547][1268]";
$cadena = preg_replace('/[^A-Za-z0-9\-]/',' ', $cadena);
$cadena=trim($cadena);
$cadena = str_replace(' ', ',', $cadena);
print_r($cadena);
to get:
158,547,1268