How can I set any quantity of new lines with a regular expression? I need to remove every new line (n), not <br/>, between two paragraphs. Answer To begin with, str_replace() (which you referenced in your original question) is used to find a literal string and replace it. preg_replace() is used to find something that matches a regular expression and
Tag: regex
Match a PHP class with a Regular Expression
I wanna catch Php classes from a file: and the result matches must be and Answer regexps are poor at parsing programming languages’ grammars. Consider tokenizer functions instead. e.g. http://php.net/manual/en/function.token-get-all.php see also this http://framework.zend.com/apidoc/core/Zend_Reflection/Zend_Reflection_File.html
preg_match() and username
Easy one.., i have this, can you explain what it checks for? I know it checks if the username have length between 2-20, what more? Thanks Answer It searches for text containing only alphanumeric and underscore characters, from 2 to 20 characters long. /^[a-zd_]{2,20}$/i |||| | ||| ||| |||| | ||| ||i : case insensitive |||| | ||| |/ :
How to remove numbers from a string with RegEx
I have a string like this: I would like to remove 23 so I’m left with PM or (with space truncated) just PM. Any suggestions? Needs to be in PHP Answer
Replacing a specific part of a query string PHP
I use $_SERVER[‘QUERY_STRING’] to get the query sting. A example would be a=123&b=456&c=789 How could I remove the b value from the query string to obtain a=123&c=789 where b can be any value of any length and is alpha numeric. Any ideas appreciated, thanks. Answer The value is going to be $_GET[‘b’]. How about:
Which regexp php is using for filter_var($url, FILTER_VALIDATE_URL)?
filter_var($url, FILTER_VALIDATE_URL) seems to be great way to detect if $url contains url or not. Is there any way to see what regular expression this function uses for detecting? Thank you
Validate class/method names with regex
I’m currently working on an MVC Style framework for a company and for security reasons I need to make sure that the controller / method that’s passed via the Query String is valid chars to the RFC (which I can’t find). I need to be able to validate / sanitize class names according to what’s allowed by the PHP interpreter
Strip all non-alphanumeric, spaces and punctuation symbols from a string
How can I use PHP to strip out all characters that are NOT letters, numbers, spaces, or punctuation marks? I’ve tried the following, but it strips punctuation. Answer Example: p{P} matches all Unicode punctuation characters (see Unicode character properties). If you only want to allow specific punctuation, simply add them to the negated character class. E.g:
How to deal with Polish Characters while using regex?
I have street name as KRZYWOŃ ANIELI and so what should be my regex to allow this kind of expression. Currently I have simple one which uses /^[a-zA-Z ]+$/ Kindly advise. Answer Use /^[sp{L}]+$/u (PHP syntax). Edit: Adjusted regex to better handle whitespace.
How to prefix a positive number with plus sign in PHP
I need to design a function to return negative numbers unchanged but should add a + sign at the start of the number if its already no present. Example: It will get only numeric input. This function is going to be called several times in echo/print so the quicker the better. Answer You can use regex as: But I would