dsaba Posted March 11, 2007 Share Posted March 11, 2007 I searched google and php.net but I suppose I don't have the right keywords for what function I want I wanted to ask if anyone knew the best method or best php function to read the begg. and end of a string. for example: $string = "<<hello world>>"; I want to make sure that the first 2 characters of this string are "<<" I also want to make sure that the last 2 characters are ">>" If this is not true then I want to reject or echo an error message. I know how to check if certain characters exist in a string, and even replace them, but I want to check if certain characters exist in the begg. or end of a string. -thanks in advance Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/ Share on other sites More sharing options...
redarrow Posted March 11, 2007 Share Posted March 11, 2007 use eregi or preg_match ok. Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/#findComment-204973 Share on other sites More sharing options...
paul2463 Posted March 11, 2007 Share Posted March 11, 2007 function check_string($str) { $num = strlen($str)-2; $begin = substr($str,0,2); $end = substr($str,$num); if (($begin == "<<")&&($end==">>")) { return true; } else { return false; } } $string = "<<hello world>>"; if(check_string($string) { //do stuff in this case as it will return true on $string } else { //error message } Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/#findComment-204977 Share on other sites More sharing options...
dsaba Posted March 11, 2007 Author Share Posted March 11, 2007 *edit (removed my question for an example, since right before I posted paul did show me an example) thanks Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/#findComment-204978 Share on other sites More sharing options...
paul2463 Posted March 11, 2007 Share Posted March 11, 2007 thats what my check_function($string) does for you function check_string($str) // pass the function the string to check { $num = strlen($str)-2; // count the number of characters in the string then assign the variable $num with this number - 2 $begin = substr($str,0,2); //strip out the first two characters and assign to $begin $end = substr($str,$num); //strip out the last two characters and assign to $end if (($begin == "<<")&&($end==">>")) { return true; } else { return false; } } Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/#findComment-204980 Share on other sites More sharing options...
dsaba Posted March 11, 2007 Author Share Posted March 11, 2007 is there also a way I can delete the rest of the string from << and on for example: $string = "<<hello world>>"; I want to keep "<<" but delete the rest also another thing I want to do is add on from there as well for example: $string = "<<hello world>>"; Only after the "<<" I want to add "hola mundo en espanol" so the string will now read "<<hola mundo en espanol hello world>>" can you show me an example with that? Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/#findComment-204982 Share on other sites More sharing options...
redarrow Posted March 11, 2007 Share Posted March 11, 2007 Be back in 5 min tell me what it all means ok? <?php $word="<< hi there i am redarrow use preg_match or eregi >>"; if (eregi("^<<.[a-z0-9]{0,100}.+>>$",$word)){ echo "correct"; }else{ echo"not correct"; } ?> Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/#findComment-204984 Share on other sites More sharing options...
dsaba Posted March 11, 2007 Author Share Posted March 11, 2007 no worries I think you meant that you will tell me what it all means? or did you want me to tell you what it means? because regex is one of my weakness, i don't know the syntax, so i try to stay away from it (for now) Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/#findComment-204985 Share on other sites More sharing options...
redarrow Posted March 11, 2007 Share Posted March 11, 2007 ARE YOU HAPPY NOW M8? Link to comment https://forums.phpfreaks.com/topic/42253-function-for-checking-begg-and-end-of-a-string/#findComment-204986 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.