preg-match

Split URL by PHP

谁都会走 提交于 2019-12-11 19:03:05
问题 Hi I'm a newbie in PHP and for my home project I need help where I want to split values with in a URL. $url = "http://localhost/Sub/Key/Key_XYZ_1234.php"; url can also be: $url = "http://localhost/Sub/Key/Key-XYZ-1234.php"; key value can be "ABC-TT" or "ABC_TT" or just "ABC" intended result $v1 = value of Key; $v2 = value of XYZ; $v3 = value of 1234; Thanks in advance 回答1: You can do this with a combination of parse_url, basename and preg_split. We use preg_split so we can deal with the

Php routing preg_match help needed

坚强是说给别人听的谎言 提交于 2019-12-11 18:34:20
问题 I have a custom routing class, which allows me to do matches like this on requests: '/[*:cat1]/[*:cat2]/?[*:cat3]/?[*:cat4]/?[p:page]/?' Which will match the following links: category-one/ category-one/cat-two/ category-one/cat-two/cat-three/ category-one/cat-two/cat-three/cat-four/ As you can see the ? after / means that parameter is optional. My problem is with [p:page]/? which is also optional. category-one/page-2/ category-one/cat-two/page-2/ category-one/cat-two/cat-three/page-2/

Regex pattern for matching single quoted words in a string and ignore the escaped single quotes

北城余情 提交于 2019-12-11 18:12:31
问题 My PHP code looks like this: $input = "City.name = 'New York'"; $literal_pattern = '/\'.[^\']*\'/'; preg_match($literal_pattern, $input, $token); echo $token[0]; // prints 'New York' My regex needs to grab literals with escaped single quotes like: $input = "City.name = 'New \' York'"; $literal_pattern = ???????????; preg_match($literal_pattern, $input, $token); echo $token[0]; // should prints 'New \' York' What wil be the reges for $literal_pattern ? 回答1: Without this condition, simple... /(

PHP regex match specific URL and strip others

穿精又带淫゛_ 提交于 2019-12-11 17:58:56
问题 I wrote this function to convert all specific URLs(mywebsite.com) to links, and strip other URLs to @@@spam@@@. function get_global_convert_all_urls($content) { $content = strtolower($content); $replace = "/(?:http|https)?(?:\:\/\/)?(?:www.)?(([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+\.[A-Za-z]+)(?:\/.*)?/im"; preg_match_all($replace, $content, $search); $total = count($search[0]); for($i=0; $i < $total; $i++) { $url = $search[0][$i]; if(preg_match('/mywebsite.com/i', $url)) { $content = str_replace(

Replacing Tags with Includes in PHP with RegExps

↘锁芯ラ 提交于 2019-12-11 17:35:04
问题 I need to read a string, detect a {VAR}, and then do a file_get_contents('VAR.php') in place of {VAR}. The "VAR" can be named anything, like TEST, or CONTACT-FORM, etc. I don't want to know what VAR is -- not to do a hard-coded condition, but to just see an uppercase alphanumeric tag surrounded by curly braces and just do a file_get_contents() to load it. I know I need to use preg_match and preg_replace, but I'm stumbling through the RegExps on this. How is this useful? It's useful in hooking

permutation using regular expression in php

让人想犯罪 __ 提交于 2019-12-11 12:18:56
问题 Is there any way to get permutation of all words in a string using only regular expression in php. For example: for input like "How to remove pain in human knee" I want output as: "How To", "How","pain knee","remove knee","knee pain","in remove","human pain knee", etc. 回答1: Using regex will only serve to slow down the process. I have written a function based on the powerSet() function posted by Yada @ https://stackoverflow.com/a/27968556/2943403) Code: (Demo) function permStacker($array){

Regular Expression for alpha numeric, space, dash, and hash

和自甴很熟 提交于 2019-12-11 10:29:21
问题 I'm still a newbie for regular expressions. I want to create a regular expression with this rule: if (preg_match('^[ A-Za-z0-9_-#]^', $text) == 1) { return true; } else { return false; } In short, I would like $text to accept texts, numbers, spaces, underscores, dashes, and hashes (#). Is the above reg expression correct? it always return true. 回答1: First off, you shouldn't use ^ as the expression boundaries, because they're also used for expression anchors; use / , ~ or # instead. Second,

regex to find number of specific length, but with any character except a number before or after it

半腔热情 提交于 2019-12-11 10:21:59
问题 I'm trying to work out a regex pattern to search a string for a 12 digit number. The number could have any number of other characters (but not numbers) in front or behind the one I am looking for. So far I have /([0-9]{12})/ which finds 12 digit numbers correctly, however it also will match on a 13 digit number in the string. the pattern should match 123456789012 on the following strings "rgergiu123456789012ergewrg" "123456789012" "#123456789012" "ergerg ergerwg erwgewrg \n rgergewrgrewg

Remove the punctuation mark with regular expression?

别说谁变了你拦得住时间么 提交于 2019-12-11 09:29:51
问题 I made this function to limit the length of a string in the output, /* limit the lenght of the string */ function limit_length($content, $limit) { # strip all the html tags in the content $output = strip_tags($content); # count the length of the content $length = strlen($output); # check if the length of the content is more than the limit if ($length > $limit) { # limit the length of the content in the output $output = substr($output,0,$limit); $last_space = strrpos($output, ' '); # add dots

Regular expression error: no ending delimiter

China☆狼群 提交于 2019-12-11 09:13:11
问题 I'm trying to execute this regular expression: <?php preg_match("/^([^\x00-\x1F]+?){0,1}/", 'test string'); ?> But keep getting an error: Warning: preg_match() [function.preg-match]: No ending delimiter '/' found in /var/www/preg.php on line 6 I can't understand where it is coming from. I have an ending delimeter right there... I tried to change delimiter to other symbols and it didn't help. I would appreciate your help on this problem. 回答1: Like Gumbo said, preg_match is not binary safe. Use