Extract word from a string from a symbol

0

I need to search in a chain, I give you an example:

"Test text #palabra blablablabla"

I would need to extract "#pablabra". I have seen that with "strpos ($ string," # ")" I can take the position where the # is, but how can I from that position extract the word that would end up in a space. That word can be anything, a name, an object etc, that's why I say that the word would end when there is a space:)

Sorry if I explain myself wrong, thank you very much in advance.

    
asked by dreambreaker 06.03.2018 в 10:54
source

1 answer

0

The ideal in this case is to use regular expressions. In your case, you want something that starts with # and contains all the letters until the next space, comma, period, etc. Something like /(\#[\wáéíóúüñ]+)/ ( \w represents all the characters that can compose a word, but does not know the accents, that's why they are added separately).

Then you apply via preg_match () and you will have all the patterns found.

<?php
$cadena = "Texto de prueba #palabra blablablabla";

preg_match('/(\#[\wáéíóúüñ]+)/g', $cadena, $patrones); // mete todo lo encontrado en el array $patrones

print_r($patrones); // Muestra los resultados

? >

    
answered by 06.03.2018 в 11:03