|
Code Examples / Notes » strchrwebmaster
Alternative Way: To get all the text before the first occurence. ----------------------------------------------- INCLUDING A NEEDLE: $string1 = "I need cookies & soda."; $needle = "cookies"; //find length of the needle $needle_len = strlen($needle); //find postion $position_num = strpos($string1,$needle) + $needle_len; //cut the string $result_string = substr("$string1",0,$position_num); //display it echo"$result_string"; // I need cookies ----------------------------------------------- SHORTER VERSION: $result_string = substr("$string1",0,strpos($string1,$needle)+strlen($needle)); echo"$result_string";//I need cookies ----------------------------------------------- EXCLUDING THE NEEDLE: $result_string = substr("$string1",0,strpos($string1,$needle)); echo"$result_string";// I need ----------------------------------------------- FREE EMAIL JUNK? This is probably useful for processing emails. For example, someone sends email to your server from Yahoo account. Free email always comes with wasted stuff like... "Do you Yahoo!? The New Yahoo! Shopping - with improved product search ". We can delete the phrase like this: $needle="Do you Yahoo!?"; $result_string = substr("$emailstring",0,strpos($emailstring, $needle)); joaobett
//If you want to get the text before the occurence of the character //you want to find, simply use the function strRev twice: $stringA = "user@example.com"; $toFind = "@"; $stringA = strrev($stringA); //The first time $result = strchr($stringA,$toFind); $result = strrev($result); //The second time echo $result; //prints user@ //You can use it this way instead: echo strrev( strchr(strrev($stringA),$toFind) ); -joao |
Change Languageaddcslashes addslashes bin2hex chop chr chunk_split convert_cyr_string convert_uudecode convert_uuencode count_chars crc32 crypt echo explode fprintf get_html_translation_table hebrev hebrevc html_entity_decode htmlentities htmlspecialchars_decode htmlspecialchars implode join levenshtein localeconv ltrim md5_file md5 metaphone money_format nl_langinfo nl2br number_format ord parse_str printf quoted_printable_decode quotemeta rtrim setlocale sha1_file sha1 similar_text soundex sprintf sscanf str_getcsv str_ireplace str_pad str_repeat str_replace str_rot13 str_shuffle str_split str_word_count strcasecmp strchr strcmp strcoll strcspn strip_tags stripcslashes stripos stripslashes stristr strlen strnatcasecmp strnatcmp strncasecmp strncmp strpbrk strpos strrchr strrev strripos strrpos strspn strstr strtok strtolower strtoupper strtr substr_compare substr_count substr_replace substr trim ucfirst ucwords vfprintf vprintf vsprintf wordwrap |