Jump to content

How to identify a word and get it into a string?


kevk3v

Recommended Posts

This is an example of what i'm trying to do...

<?php $text = '%$%#$567hello!@#$%^& how@#$%%%$ are#$% you?'; //ok lets say i have this <-
//i want to find the word in $text without having the word and then get it into a string
//example: i have "h" and i want to find the word in $text that begins with "h" and get that word into a string
?>
how do i do this?, sorry for my bad english btw...

If you know there there wont be randomly jumbled letters in the mix, then you can use a simple PCRE regular expression:

 

<?php

 

$string = '%$%#$567hello!@#$%^& how@#$%%%$ are#$% you?';

 

// Get any string of 1 or more characters between a and z (with spaces).

preg_match_all("/[a-z ]+/i",$string,$words);

 

// echo results;

foreach($words as $word){

  echo($word."<br />");

}

?>

 

-cb-

wow thanks  :rtfm: ok lets say i have some russian text example $string = "текст///вау:?23прохладный";

how can i get preg match to work with the russian text?  :)

you got exactly what i was looking for but what if i want to find text in russian ?

If you know there there wont be randomly jumbled letters in the mix, then you can use a simple PCRE regular expression:

 

<?php

 

$string = '%$%#$567hello!@#$%^& how@#$%%%$ are#$% you?';

 

// Get any string of 1 or more characters between a and z (with spaces).

preg_match_all("/[a-z ]+/i",$string,$words);

 

// echo results;

foreach($words as $word){

  echo($word."<br />");

}

?>

 

-cb-

 

Why bother writing all this as it's already built-in

 

$text = '%$%#$567hello!@#$%^& how@#$%%%$ are#$% you?';
print_r(str_word_count($text, 1));

 

ok lets say i have some russian text example $string = "текст///вау:?23прохладный";

 

The same applies:

 

setlocale(LC_COLLATE, 'ru_RU');

$text = 'текст///вау:?23прохладный';
print_r(str_word_count($text, 1));

Actually it doesn't work at all, it only displays english i want it to display russian only, help me please :(

 

What do you mean? Try setlocale (LC_ALL, array ('ru_RU.CP1251', 'rus_RUS.1251')); This will match words if you use a function like str_word_count() it will not translate them.

Actually it doesn't work at all, it only displays english i want it to display russian only, help me please :(

 

What do you mean? Try setlocale (LC_ALL, array ('ru_RU.CP1251', 'rus_RUS.1251')); This will match words if you use a function like str_word_count() it will not translate them.

it's not translating example i use

<?php setlocale(LC_ALL, array('ru_RU.CP1251', 'rus_RUS.1251'));
$text = "тест,test";
print_r(str_word_count($text, 1));
?>

it shows "test" instead of the russian word and привеÑ

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.