itsureboy Posted February 26, 2011 Share Posted February 26, 2011 Hey, I need a simple bit of code to do the following: I am trying to strip out a username in a string. This string will distinguish the username because the string will contain a * right before the username. For example: $string = "blah blah *username blah blah blah" The username can only contain numbers and letters. no spaces. And from that string all I want is the username, for example: $string = username (which will vary of course) Hope this makes sense, made it as clear as I possibly could. Thanks ahead! :confused: Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/ Share on other sites More sharing options...
litebearer Posted February 26, 2011 Share Posted February 26, 2011 will the number of *'s vary? Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/#findComment-1179932 Share on other sites More sharing options...
daFreak Posted February 26, 2011 Share Posted February 26, 2011 I would suggest you check out the preg_match() function. Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/#findComment-1179933 Share on other sites More sharing options...
BluB Posted February 26, 2011 Share Posted February 26, 2011 Something like this should also do the trick: Not as optimized, but a bit more simple then using preg_match() imho. //look for the first occurrence of a * for($i = 0; substr($string, $i, 1) != "*"; $i++) { } //look for the first occurrence of a space after the * for($j = $i; substr($string, $j, 1) != " "; $j++) { } //retrieve username from string $username = substr($string, $i +1, $j -$i); Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/#findComment-1179962 Share on other sites More sharing options...
litebearer Posted February 26, 2011 Share Posted February 26, 2011 OR IF only 1 * - explode once using * as delimiter, then take element 1 (NOT 0) and explode that using the space as delimiter. element 0 of the new array will have what is proposed as the user name - check it for invalid characters Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/#findComment-1179988 Share on other sites More sharing options...
.josh Posted February 26, 2011 Share Posted February 26, 2011 $string = 'blah *user blah'; // string to look for username in preg_match('~\*([a-z0-9]+)~i',$string,$username); $username = $username[1]; Though you are going to get false results from this, depending on your string. For example: $string = "he *probably* meant to say *username was a retard"; The code will return "probably". Unfortunately, there isn't really anything you can do about something like that... Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/#findComment-1179996 Share on other sites More sharing options...
mattal999 Posted February 26, 2011 Share Posted February 26, 2011 $string = 'blah *user blah'; // string to look for username in preg_match('~\*([a-z0-9]+)~i',$string,$username); $username = $username[1]; Though you are going to get false results from this, depending on your string. For example: $string = "he *probably* meant to say *username was a retard"; The code will return "probably". Unfortunately, there isn't really anything you can do about something like that... This is completely untested, and is probably not going to work, but couldn't you use a negative lookahead? $string = 'blah *user blah'; // string to look for username in preg_match('~\*([a-z0-9]+)(?!\*)~i',$string,$username); $username = $username[1]; Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/#findComment-1180030 Share on other sites More sharing options...
.josh Posted February 26, 2011 Share Posted February 26, 2011 No not really...Lookaheads (or other methods, like preg_match_all(), etc... will simply tell you things like if there is more than one instance of * in the string. Or in your example, only match if there are no other instances of *. The point I was trying to make is that you can have two different instances of * being used in a string and there's no way to code to know which one is a username and which one is some other random use, since they are both arbitrary values. Or in your example, yes, it checks if there are no other instances of *, but there's no way to know whether or not that one instance is a username. Or what if the username is mentioned twice in the string? Or two different usernames mentioned? Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/#findComment-1180045 Share on other sites More sharing options...
mattal999 Posted February 26, 2011 Share Posted February 26, 2011 I suppose you're right - it was only a small suggestion because you sometimes see emotions or certain words encapsulated in asterisks in posts. Of course, there really is no concrete solution to this problem but every little helps Quote Link to comment https://forums.phpfreaks.com/topic/228901-php-to-locate-certain-text-in-string/#findComment-1180046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.