Jump to content

PHP Regex: Match numbers within symbols?


Brad R

Recommended Posts

Hi, I'm new. I've read the stickies, and couldn't find what I was looking for as I am so new to Regex that I was totally lost, lol. I even tried regexlib.net to no avail.

 

I want to match <<####>> # = numbers, obviously, it can be any amount of numbers 0 - infinite.

 

This is what I have been trying

$pattern = "\<\</[^0-9]/\>\>";

And that doesn't work, It just matches all numbers, but I need them only to match if it is in the symbols << and >> and yes those are double < and >

 

I am using this for an IRC bot. and the numbers represent topic numbers, and I am using the << and >> to trigger the command.

 

Any help is appreciated.

Million thanks in advance. :)

Sorry if I didn't see something, feel free to call me a stupid loser and throw a topic link in my face. :P

 

PS: I suppose I could use just one < and >, that's not a problem for me if it is a problem to use double < and >

Link to comment
https://forums.phpfreaks.com/topic/162857-php-regex-match-numbers-within-symbols/
Share on other sites

Thanks for your time, but that didn't seem to work. Here is the entire code, if this helps.

 

<?php
class plugGTopic{
    	public static function onText(
	event $event,
	$dh = null) {
        //Now I gotta get the title
        $subject = $dh->getircTrailing();
	//$pattern = "\<\</[^0-9]/\>\>"; // These 3 $patterns have all been failures.
	//$pattern = '#<<\d+?>>#';
	//$pattern = "/\<\<([0-9]?([^\]]+)\>\>/i";
	if(preg_match_all($pattern, $subject, $matches)) {
		$dh->reply('Nice try, '.$dh->getIrcPrefixNickname().' but the parameters you entered: "'.$dh->getircTrailing().'" must not be number(s). You have to use a number, hence the parameters of this command which are: <topic _NUMBER_ >.');
	} else {
	$page = file_get_contents('http://forums.tyreus.com/index.php?showtopic='.$dh->getircTrailing());
	$title = explode("<title>", $page);
	$title = explode("</title>", $title[1]);
	$title = explode(" - ", $title[0]);

	//Shout it out!
		if($title[0] == "Board Message"){
			$dh->reply('Sorry '.$dh->getIrcPrefixNickname().', But topic number '.$dh->getircTrailing().' isn\'t found!');
		}
		else{
			$dh->reply(''.$title[0].' - http://forums.tyreus.com/index.php?showtopic='.$dh->getircTrailing().'');
		$event->delete();
		}
	}
}
}

If it can be any amount of numbers, use an * instead of a + in the pattern. So if I read your code right, the script should trigger an error if e.g. <<>> or <<866789>> is found anywhere in $subject? Is that right? I can't make sense of it when I read the error message, saying "(...) the parameters you entered must not be number(s). You have to use a number (...)".

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.