determine the position of a character in a string

2

hello my question is how can I determine the position of the question mark in this string * and then separate it with the function substr()

string = [Catálogo en línea?http://ur_prueba/index.jsp ]
    
asked by carlos becerra 26.09.2018 в 22:31
source

3 answers

2

I think it would be more appropriate to use explode () , using "?" how to delimit between the 2 string, in this way you get an array containing both parts:

  $string = "[Catálogo en línea?http://ur_prueba/index.jsp ]";      
  $arr=explode("?",$string); 

like this:

  • $arr[0] would contain the value: "[Catálogo en línea"
  • $arr[1] would contain the value: "http://ur_prueba/index.jsp ]"

Example:

<?php
    $string = "[Catálogo en línea?http://ur_prueba/index.jsp ]";      
     $arr=explode("?",$string); 
     echo 'Primera parte: '. $arr[0] . '<br>Segunda parte: '. $arr[1]  
?>

To get as output:

Primera parte: [Catálogo en línea
Segunda parte: http://ur_prueba/index.jsp]
    
answered by 26.09.2018 / 22:50
source
3

You have several options:

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Nótese el uso de ===. Puesto que == simple no funcionará como se espera
// porque la posición de 'a' está en el 1° (primer) caracter.
if ($pos === false) {
    echo "La cadena '$findme' no fue encontrada en la cadena '$mystring'";
} else {
    echo "La cadena '$findme' fue encontrada en la cadena '$mystring'";
    echo " y existe en la posición $pos";
}
?>

Option 2:

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// El operador !== también puede ser usado. Puesto que != no funcionará como se espera
// porque la posición de 'a' es 0. La declaración (0 != false) se evalúa a 
// false.
if ($pos !== false) {
     echo "La cadena '$findme' fue encontrada en la cadena '$mystring'";
         echo " y existe en la posición $pos";
} else {
     echo "La cadena '$findme' no fue encontrada en la cadena '$mystring'";
}
?>

For more information check the PHP documentation:

PHP Documentation

For me it is one of the best in terms of languages

    
answered by 26.09.2018 в 22:41
2

With the "strpos" function you can get the position where the character you are looking for is, I suppose you want to extract the url, you also have to remove the square parenthesis at the end, and the condigo would be this way

$string = "[Catálogo en línea?http://ur_prueba/index.jsp]"; 
$posicion = strpos ( $string, '?' );
$tamTotal = strlen($string);
echo substr($string,$posicion+1,$tamTotal-$posicion-2);
    
answered by 26.09.2018 в 22:49