Jump to content

PHP to locate certain text in string


itsureboy

Recommended Posts

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: :confused:

Link to comment
Share on other sites

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);

 

 

Link to comment
Share on other sites

$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...

Link to comment
Share on other sites

$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];

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.