RSS

Regex di PHP

28 Sep

Kemarin lagi cari-cari Regex di PHP, sudah lama tidak punya masalah di Regex, karena butuh Panduan Regex di PHP terpaksa search lagi di Om google deh, ni artikel-a.

The following code, the way I understand it, is supposed to strip all punctuation characters from the string. From what I’ve found on the web, a space is not one of those characters. But the code below outputs “abstractmodern.” Would someone please explain how I can strip all special characters except the spaces?

PHP Code:

$pattern = "([[:punct:]])"; 
$str = "abstract modern"; 
$str = ereg_replace($pattern, '', $str));
echo $str;  




source :http://www.talkphp.com/general/4758-using-regex.html

POSIX Character Class Definitions

Value

Meaning

[:digit:] Only the digits 0 to 9
[:alnum:] Any alphanumeric character 0 to 9 OR A to Z or a to z.
[:alpha:] Any alpha character A to Z or a to z.
[:blank:] Space and TAB characters only.
[:xdigit:] Hexadecimal notation 0-9, A-F, a-f.
[:punct:] Punctuation symbols . , ” ‘ ? ! ; : # $ % & ( ) * + – / < > = @ [ ] \ ^ _ { } | ~
[:print:] Any printable character.
[:space:] Any whitespace characters (space, tab, NL, FF, VT, CR). Many system abbreviate as \s.
[:graph:] Exclude whitespace (SPACE, TAB). Many system abbreviate as \W.
[:upper:] Any alpha character A to Z.
[:lower:] Any alpha character a to z.
[:cntrl:] Control Characters NL CR LF TAB VT FF NUL SOH STX EXT EOT ENQ ACK SO SI DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC IS1 IS2 IS3 IS4 DEL.

These are always used inside square brackets in the form [[:alnum:]] or combined as [[:digit:]a-d]

Example
Replace URLs with links
$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
"\", $text);?>

source : http://php-regex.blogspot.com/

Advertisement
 
Leave a comment

Posted by on September 28, 2011 in PHP

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.