Skip to content
Advertisement

Tag: regex

How to replace new lines by regular expressions

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

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 |||| | ||| |/ :

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:

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

Advertisement