Jump to content

Recommended Posts

how can i make preg_match get the username inside of this and just ignoring the id number. (note: the id number always varies...)

 

<a href="member.php?u=1">user</a>

 

here's the code that im using atm but its not working :s

 

<? preg_match_all('|<a href="member.php?u=([0-9])">(.*?)</a>|mU',$data,$result); ?>

 

any ideas?

Link to comment
https://forums.phpfreaks.com/topic/172894-solved-preg_match-help/
Share on other sites

If the value being searched is ONLY in the format you entered and is not embedded in a larger body of text (specifically with other < or >), then this will work just fine:

 

preg_match("/>(.*)</", $link, $matches);
$username = $matches[1];

 

If the username is within larger bodies of text then you can use this

 

preg_match("/<a href=\"member\.php\?u=\d+\">(.*)<\/a>/", $link, $matches);
$username = $matches[1];

 

The will grab the 'text' of an anchor only if it is exactly formatted as you have above - also the ID must consist of one or more numbers.

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.