Skip to content
Advertisement

Tag: preg-match

How to use preg_match to match numbers between hyphens?

I need to validate strings like this: I need the penultimate number between hyphens, so in this case ’10’. These are other examples: Can you help me? Answer this RegExp should do the trick using a positive lookahead: (change 4 to whatever max num of digits you want to allow)

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

How to search in an array with preg_match?

How do I search in an array with preg_match? Example: Answer In this post I’ll provide you with three different methods of doing what you ask for. I actually recommend using the last snippet, since it’s easiest to comprehend as well as being quite neat in code. How do I see what elements in an array that matches my regular

Scraping a messy html website with PHP

I am in the following situation. I am trying to convert a messy scraped html code to a nice and neat xml structure. A partial HTML code of the scraped website: Now I want to create the following xml structure with php: Have been trying the simple html dom method, but have no idea how to get the next sibling

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

Using regex to extract username from email address

My string of text looks like this: I need to get just the part before the @ and nothing else. The text is coming from a simple XML object if that matters any. The code I have looks like this: Sometimes the username might have numbers or an underscore before the @ symbol, which is where the regex stops it

Advertisement