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

Link to comment
Share on other sites

Uh, it should work. Can you var_dump $subject?

 

Here's a test run I created:

$ more file.php
<?php
$e = '<<9372409210943>>';
preg_match('#<<\d+?>>#', $e, $match);
var_dump($match);

$ php file.php 
array(1) {
  [0]=>
  string(17) "<<9372409210943>>"
}

Link to comment
Share on other sites

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 (...)".

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.