Regex not matching spaces.

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
dotancohen
Forum Newbie
Posts: 11
Joined: Thu Aug 04, 2005 5:37 am
Location: Haifa
Contact:

Regex not matching spaces.

Post by dotancohen »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm parsing a body of text for tags that use square brackets to identify themselves, like BBCode. These tags may contain alphanumeric characters, spaces, apostrophes, underscores, dashes, basic punctuation, and pipes. The closest that I've been able to come up with is:

Code: Select all

$text=preg_replace_callback('/\[([A-Za-z0-9\¦\'.-]+)\]/i' , "findLinks", $text);
However, this does not match spaces for some odd reason (the "." should match them, I think). I've added "\s", "\w", " ", and ":space:" to the regex (tried both before the A-Z and after the 0-9) but for whatever reason those spaces are not detected. Why? What must I do?

I'm certain that what I think is a space is most certainly a space. str_replace(" ", "", $text); closes the spaces, so I know that php does in fact see it as a space.

I'm on php 5.x if it's relevant. Thanks in advance for any assistance.

Dotan Cohen
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

#\[(A-Za-z0-9\|\' _-]+)\]#
should do it.
dotancohen
Forum Newbie
Posts: 11
Joined: Thu Aug 04, 2005 5:37 am
Location: Haifa
Contact:

Post by dotancohen »

Thanks, Feyd. I used the code tags instead of php tags, sorry. I'll be more careful next time.

Your code also does not match tags with spaces. Also, on the tags that it _does_ catch, it does not pass to the function.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Basic spaces will be caught by that regex.
dotancohen
Forum Newbie
Posts: 11
Joined: Thu Aug 04, 2005 5:37 am
Location: Haifa
Contact:

Post by dotancohen »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


For me they are not. Try this:

Code: Select all

<?php

$makeLinkPlaceHolder=0;

function findLinks ($matches) {
    global $linkTextArrayPlaceHolder;
    ++$linkTextArrayPlaceHolder;

    // Make two peices and fill them with what they should be
    $matches[0]=str_replace("[", "", $matches[0] );
    $matches[0]=str_replace("]", "", $matches[0] );
    $parts=explode("|", $matches[0]);

    print "<br /><br />$parts[0] | $parts[1]";

    $returnString="[".$linkTextArrayPlaceHolder."]";
    return $returnString;
}

$text="This is some text.
This tag has no spaces and no pipes [TestTag]
This tag has no spaces and a pipe [TestTag|AfterPipe]
This tag has a space and no pipes [Test Tag]
This tag has a space and a pipe [Test Tag|AfterPipe]";

// Clean up the text
$text=str_replace("\r\n", "\n", $text);
$text=str_replace("\r", "\n", $text);
$text=str_replace("\n", "<br />", $text);

print $text;

// THIS IS THE PROBLEMATIC CODE
$text=preg_replace_callback('/\[(A-Za-z0-9\|\' _-]+)\]/i' , "findLinks", $text); 

print "<br /><br />".$text;
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$text=preg_replace_callback('/\[(A-Za-z0-9\|\' _-]+)\]/i' , "findLinks", $text);
missing a [ before A-Z
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops. :roll:
Post Reply