This is a sentence sanitizer. This is the test sentence: hello [[[[[[]]]]]] friend…..? how are you [}}}}}} It should return: hello friend…..? how are you But instead it is returning: hello friend. .. .. ? how are you. So there are 2 problems and I can’t find a solution around them: the set of periods are being separated into “..
Tag: regex
Matching all three kinds of PHP comments with a regular expression
I need to match all three types of comments that PHP might have: # Single line comment // Single line comment /* Multi-line comments */ Something I should mention: I am doing this in order to be able to recognize if a PHP closing tag (?>) is inside a comment or not. If it is then ignore it, and
How can i “merge” these two regular expression in PHP?
I’m learning regular expression, so please go easy with me! Username is considered valid when does not start with _ (underscore) and if contains only word characters (letters, digits and underscore itself): In order to merge them in one regular expression, i’ve tried the following: To be read as: add a violation if starts with an underscore (eventually more than
Get coordinates from script tag, image tag, link
I’m building a search engine for deals and I put all deals on a map, so I need coordinates from websites with scraping. So, coordinates can be on scrit, tag, image, link, etc. Is there any tool or any script, framework, that help me to quick get coordinates from some web sites ? How to do that? With PHP, XPath,
preg_match bbcode
I currently have bbcode like this [caption=Some text goes here]image.jpg[/caption] I’d like to use php’s preg_match so I can get the value of the image.jpg, regardless of what’s next to ‘caption=’. …
Wrap first word in tag with preg_replace — can’t reference fullstring match
I generated the following regex code with http://gskinner.com/RegExr/ where it works, but when I execute it with PHP, it fails to use the match in the replacement string. Output: Expected: I know that obviously the $& is not doing the correct thing, but how can I get it to work? Answer Try with this instead $0 means it will become
Insert space after semi-colon, unless it’s part of an HTML entity
I’m trying to insert a space after each semi-colon, unless the semi-colon is part of an HTML entity. The examples here are short, but my strings can be quite long, with several semi-colons (or none). I found the following regular expression that does the trick for short strings: However, if the string is somewhat large, the preg_replace above actually crashes
Replacing x or more dots
I want a clean solution to replace dots in text: Some title…. to this: Some title… Some…. title…… to this: Some… title… How can I replace every sequence of more than 3 dots with 3 dots? Answer With a regular expression based search and replaceDocs: The pattern says: Match four or more dots ., the second parameter is the replacement.
How long can a TLD possibly be?
I’m working on an email validation regex in PHP and I need to know how long the TLD could possibly be and still be valid. I did a few searches but couldn’t find much information on the topic. So how long can a TLD possibly be? Answer DNS allows for a maximum of 63 characters for an individual label.
Test if a regular expression is a valid one in PHP
I am writing a form validation class and wish to include regular expressions in the validation. Therefore, the regex provided isn’t guaranteed to be valid. How can I (efficiently) check that the …