preg-match

Warning: preg_match(): Compilation failed: unrecognized character after (? or (?-

别来无恙 提交于 2019-12-01 07:32:59
问题 I have a code written by a previous programmer, a regex preg_match that is throwing a compilation error: $regex_t = "/" . $op . "(?\\>[^" . $op . $cl . "]+)*" . $cl . "/s"; preg_match($regex_t, $text, $inner); The warning that I receive is: Warning: preg_match(): Compilation failed: unrecognized character after (? or (?- at offset 4 Also, I want to mention that at var_dump($regex_t) the value is: string '/\{(?\>[^\{\}]+)*\}/s' (length=21) 回答1: For a strange reason, the programmer has escaped

php extract Emoji from a string

我是研究僧i 提交于 2019-12-01 06:49:41
I have a string contain emoji. I want extract emoji's from that string,i'm using below code but it doesn't what i want. $string = "😃 hello world 🙃"; preg_match('/([0-9#][\x{20E3}])|[\x{00ae}\x{00a9}\x{203C}\x{2047}\x{2048}\x{2049}\x{3030}\x{303D}\x{2139}\x{2122}\x{3297}\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F6FF}

Regular expression to detect Internet Explorer 11

荒凉一梦 提交于 2019-12-01 03:48:56
I am using this preg_match string preg_match('/Trident/7.0; rv:11.0/',$_SERVER["HTTP_USER_AGENT"] to detect IE11 so I can enable tablet mode for it. However it returns "unknown delimiter 7". How can I do this without PHP complaining at me? Is this really a regular expression or just a literal string? If it's just a string, you can use the strpos function instead. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false) { // your code } If it's a regular expression, you must escape special characters like / and . EDIT: You can see in the comments of this answer that the code

Get all matches from string that start with and end, using php

岁酱吖の 提交于 2019-12-01 03:27:53
How would I use PHP to extract everything in between [startstring] and [endstring] from this string: [startstring]hello = guys[endstring] hello guys [startstring]jerk = you[endstring] welcome to the universe I'd like to find all the matches of [startstring] and [endstring] , and echo the text inside. Kinda like a delimiter. And I would like all the matches echoed to be seperated by a space. How would I go about accomplishing this? Would this require regex ? Could you provide a sample? Thanks so much! :) preg_match_all('/\[startstring\](.*?)\[endstring\]/s', $input, $matches); echo implode(' ',

Reg exp for youtube link

帅比萌擦擦* 提交于 2019-12-01 01:50:33
问题 In a system that I'm developing I need to recognize the youtube link in the following format [youtube]youtube url[/youtube] for the moment I arrived at this regular expression: #\[youtube\]http://www.youtube\.(.*)/watch\?v=([a-zA-Z0-9_-]*)\[\/youtube\]#s But this pattern isn't able to recognize url like [youtube]http://www.youtube.com/watch?v=s3wXkv1VW54&feature=fvst[/youtube] Note the feature=fvst . Some one can help me in recognize all possible youtube url? 回答1: How about |\[youtube\]http:/

php preg_match return position of last match

我的梦境 提交于 2019-12-01 00:18:10
问题 With preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE); is it possible to search a string in reverse? ie. return the position of the last occurrence of the pattern in the subject similar to strripos Or do i have to return the position of all matches with preg_match_all and use the last element of $matches ? 回答1: PHP doesn't have a regex method that search a string from right to left (like in .net). There are several possible recipes to solve that (this list isn't exhaustive, but

How to extract Heading tags in PHP from string

本小妞迷上赌 提交于 2019-11-30 21:00:42
From a string that contains a lot of HTMl, how can I extract all the text from <h1><h2>etc tags into a new variable. Possibly using preg_match_all and sending the matches to a single comma delimited variable. Thanks guys. Scott Saunders If you actually want to use regular expressions, I think that: preg_match_all('/<h[0-6]>([^</h[0-6]>*)</h/i', $string, $matches); should work as long as your header tags are not nested. As others have said, if you're not in control of the HTML, regular expressions are not a great way to do this. First you need to clean up the HTML ($html_str in the example)

how to remove multiple slashes in URI with 'PREG' or 'HTACCESS'

天涯浪子 提交于 2019-11-30 20:17:01
how to remove multiple slashes in URI with 'PREG' or 'HTACCESS' site.com/edition/new/// -> site.com/edition/new/ site.com/edition///new/ -> site.com/edition/new/ thanks using the plus symbol + in regex means the occurrence of one or more of the previous character. So we can add it in a preg_replace to replace the occurrence of one or more / by just one of them $url = "site.com/edition/new///"; $newUrl = preg_replace('/(\/+)/','/',$url); // now it should be replace with the correct single forward slash echo $newUrl $url = 'http://www.abc.com/def/git//ss'; $url = preg_replace('/([^:])(\/{2,})/',

Regex to validate date in PHP using format as YYYY-MM-DD

做~自己de王妃 提交于 2019-11-30 19:47:07
I am trying to make a date regex validator. The issue I'm having is that I'm using an input field with "date" type, which works like a charm in Chrome; it opens a calendar-like in Chrome, but in the rest it does nothing, so I decided to go for a manual input of the date for the rest. This is my error throwing message (I'm looking for YYYY-MM-DD format): $date_regex ='#^(19|20)\d\d[\- /.](0[1-9]|1[012])[\- /.](0[1-9]|[12][0-9]|3[01])$#'; $hiredate = $_POST['hiredate']; if (!preg_match($date_regex, $hiredate)){ $errors[] = 'Your hire date entry does not match the YYYY-MM-DD required format.'; }

Regex split email address

荒凉一梦 提交于 2019-11-30 18:07:12
I need some help with php regex, I want to "split" email address "johndoe@example.com" to "johndoe" and "@example.com" Until now I have this: preg_match('/<?([^<]+?)@/', 'johndoe@example.com', $matches); And I get Array ( [0] => johndoe@ [1] => johndoe) So how I need to change regex? $parts = explode('@', "johndoe@example.com"); $user = $parts[0]; // Stick the @ back onto the domain since it was chopped off. $domain = "@" . $parts[1]; Some of the previous answers are wrong, as a valid email address can, in fact, include more than a single @ symbol by containing it within dot delimited, quoted