una separar remove recorrer parte length extraer eliminar caracteres caracter cadena buscar php string

separar - remove html tags php



¿Cómo eliminar parte de una cadena? (9)

¿Cómo recortar una parte de una cadena y guardarla en una cadena particular en MySQL usando PHP?

Ejemplo de cadena: "REGISTER 11223344 here"

¿Cómo puedo eliminar "11223344" de la cadena de ejemplo?


Cuando necesite una coincidencia basada en reglas, debe usar expresiones regulares

$string = "REGISTER 11223344 here"; preg_match("/(/d+)/", $string, $match); $number = $match[1];

Eso coincidirá con el primer conjunto de números, por lo que si necesita ser más específico, intente:

$string = "REGISTER 11223344 here"; preg_match("/REGISTER (/d+) here/", $string, $match); $number = $match[1];


Dinámicamente, si quiere eliminar (a) parte (s) de (a) índice (es) fijo (s) de una cadena, use esta función:

/* * Remove index/indexes from a string, delimited by a space (or something else). * First, divides the string by delimiter and holds divided parts as an array. * If $index is an array, it removes all indexes of the divided string; * otherwise (i.e. has an int value), it removes just that index of the string. * At last, it joins all string parts together and return it as a string. * Note: If you want to use this function in PHP5, remove scalar type hints * (i.e. keywords before function arguments). * Note: For PHP versions lower than 7.0, remove scalar type hints (i.e. the * types before each argument) and the return type. */ function remove_from_str(string $str, $index, string $delimiter = " "): string { // String parts $strParts = explode($delimiter, $str); // Remove indexes from string parts if (is_array($index)) foreach ($index as $i) $strParts[(int)$i] = null; else $strParts[(int)$index] = null; // Remove null values, to prevent duplicating delimiter $strParts = array_filter($strParts); // Join all parts together and return it return implode($delimiter, $strParts); }

Para su propósito:

remove_from_str(1, "REGISTER 11223344 here"); // Output: REGISTER here

Uno de sus usos es ejecutar cadenas de comandos, que usted conoce su estructura.


Haz lo siguiente

$string = ''REGISTER 11223344 here''; $content = preg_replace(''/REGISTER(.*)here/'','''',$string);

Esto devolvería "REGISTRAR aquí"

o

$string = ''REGISTER 11223344 here''; $content = preg_replace(''/REGISTER (.*) here/'','''',$string);

Esto devolvería "REGISTRARSE aquí"


Podrías hacer esto también, si te gustan otras formas ...

function str_substract($remove, $subject){ $strpos = strpos($subject, $remove); return substr($subject, 0, $strpos) . substr($subject, $strpos + strlen($remove)); } $str = "Hello, world. This is amazing"; print str_substract(''rld'', $str); /* Outputs: */ "Hello, wo. This is amazing"


Puede usar str_replace() , que se define como:

str_replace($search, $replace, $subject)

Entonces podrías escribir el código como:

$subject = ''REGISTER 11223344 here'' ; $search = ''11223344'' ; $trimmed = str_replace($search, '''', $subject) ; echo $trimmed ;

Si necesita una mejor coincidencia mediante expresiones regulares, puede usar preg_replace() .


Si se dirige específicamente al "11223344", entonces use str_replace :

echo str_replace("11223344", "", "REGISTER 11223344 here");


asumiendo que 11223344 no es constante

$string="REGISTER 11223344 here"; $s = explode(" ",$string); unset($s[1]); $s = implode(" ",$s); print "$s/n";


si quieres eliminar la parte de la cadena de una cadena dada, esto podría ayudarte

chop($string,''part_of_string'');


substr () es una función php incorporada que devuelve parte de una cadena. La función substr () tomará una cadena como entrada, el formulario de índice donde desea que se recorte la cadena. y un parámetro opcional es la longitud de la subcadena. Puede ver la documentación adecuada y el código de ejemplo en http://php.net/manual/en/function.substr.php

NOTA: el índice de una cadena comienza con 0.