954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple(?) Regex Problem

Hello,

I tried to use regex to search through

blablablabla http://www.myspace.com/username

by using

preg_match('~http://www\.myspace\.com/.+"~',$string,$match);

The tilde or lack of was tripping me up... am I even close?
I was just trying to extract the username at the end, before it was marked off by a double quote ("). I tried adapting this answer from a previous forum post but it doesn't seem to be working. Any help would be appreciated.

Thanks,

L

beginninphp
Newbie Poster
5 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Well if you're trying to extract the username you can just do this.

$url = 'http://www.myspace.com/username';
$urlparts = explode('/', $url);
$username = end($urlparts);
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Thanks so much, I'll test it out and see if I can get it working, I should be able to.

beginninphp
Newbie Poster
5 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

I don't know how to tie it in to

else {

	$timeout = 300;

	ini_set('max_execution_time','120');

	set_time_limit(120);

	ini_set('default_socket_timeout','120');

	while($contents = fread($handle,'1024'))

	{ $hold .= $contents; }


above it. Could anyone tell me what function the ~ serves? I don't see it in any regex guides.

beginninphp
Newbie Poster
5 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

The tilde has no special meaning, it was just chosen as the delimiter so the slashes didn't have to be escaped. You can choose pretty much any delimiter.

$regex1 = '/http:\/\/www\.example.com\/blah/';
// vs
$regex2 = "#http://www\.example\.com/blah#';

I prefer to use / and # but it looks like the tilde was their choice.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 
preg_match('~http://www\.myspace\.com/(.*)"~',$hold,$match);

This worked for me but thanks for the info on the tildes... regex isn't that bad with a running start.

beginninphp
Newbie Poster
5 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You