Jump to content

Incorrect usage of Preg_Match?


3raser

Recommended Posts

Hia. I want to ask a quick question: Should I be using preg_match or preg_match_all for the following function?  Since PHP doesn't support the global & multi-line flag in expressions, my code fails (the if statement) and it returns nope.avi. I know preg_match_all can use globals, but the third argument is a bit confusing.

 

function add_quotes($string, $username)
{
if(acc_status($username) == 3)
{
	if(preg_match('/\[quote=\w+\]/', $string) && preg_match('/\[\/quote\]/', $string))
	{

	}
	else
	{
		$string.= '<br/><br/><br/><span style="color:red">NOPE.AVI</span>';
	}
}

return $string;
}

Link to comment
https://forums.phpfreaks.com/topic/263575-incorrect-usage-of-preg_match/
Share on other sites

preg_match_all() basically is the /g flag. Unfortunately I'm too tired to be able to explain it right so that's the best sentence I've come up with yet.

 

Meanwhile /g and /m have nothing to do with your problem. Global only matters when you're trying to get the matches themselves (you're just testing that one exists) and multiline mode only affects end- and beginning-of-line anchors (you don't use any). What is the exact value of $string?

Thanks, that clears quite a bit up. I read more on the preg_replace documentary and I noticed the use of groups. Yet for some reason the following doesn't work:

 

function add_quotes($string, $username)
{
if(acc_status($username) == 3)
{
	//convert QUOTE BBcode to actual HTML format
	$string = preg_replace('/\[quote\=(.+?)](.+?)\[\/quote\]/s', "$1 said $2", $code_treated); 
}

return $string;
}

 

$string =

test

 

I wanted to test the groups, but for some reason I can't get them the replace properly. It just replaces the entire $string with a blank message.

 

EDIT: Never mind! I made simple errors in the code above. Solved!

Archived

This topic is now archived and is closed to further replies.

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