PHP substr () Función : Aquí, vamos a aprender acerca de la función substr () con el ejemplo en PHP .
PHP substr () Función
función
substr () es una función de cadena en PHP, que se utiliza para obtener la subcadena del índice especificado de la cadena.
Sintaxis:
substr(string, start, [length]);
Aquí,
- cadena es la cadena class.
- comenzar es el índice de inicio de la cadena, desde donde vuelve a subcadena.
- longitud es un parámetro opcional, define el número de caracteres a class.
Nota: Los valores negativos puntos al final de la cadena. main ejemplo: -1 puntos último carácter, -2 puntos segundo último carácter, -3 puntos tercio de caracteres pasada y así sucesivamente …
Ejemplos:
Input:
str = "Hello friends how are your friends?";
start =10
Output:
"nds how are your friends?"
str = "Hello friends how are your friends?";
start =10
length = 5
Output:
"nds h"
código PHP:
<?php
$str = "Hello friends how are your friends?";
//returns substring from 10th index to end of the string
$substring = substr($str,10);
echo ($substring . "n");
//returns substring from 10th index to next 5 chars
$substring = substr($str,10,5);
echo ($substring . "n");
//testing with negative values
$substring = substr($str,-1);
echo ($substring . "n");
//testing with negative values
$substring = substr($str,10,-1);
echo ($substring . "n");
//testing with negative values
$substring = substr($str,-5);
echo ($substring . "n");
//testing with negative values
$substring = substr($str,3,-5);
echo ($substring . "n");
?>
salida
nds how are your friends?
nds h
?
nds how are your friends
ends?
lo friends how are your fri