preg-match

PHP url validation + detection

二次信任 提交于 2019-12-10 12:13:57
问题 So here is what I need to do. If an user enters this: http://site.com I need to remove http:// so the string will be site.com , if an user enters http://www.site.com I need to remove http://www. or if the user enters www.site.com I need to remove www. or he can also enter site.com it will be good as well. I have a function here, but doesn't work how I want to, and I suck at regex. preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $_POST['link']) 回答1: I don't think I'd use

Text to html ratio of a page issue

柔情痞子 提交于 2019-12-10 12:12:14
问题 I am trying to get Text to HTML Ratio on a given webpage. I am using a strip_html_tags to strip out the html tags and comparing it to the original content on the page to get the ratio. My issue is that I feel like my strip_html_tags function may not get all the tags on webpage. Is there a better way to do this... maybe that just replaces everything that starts with < and >. I can already point out that I am missing a lot of tags that should be stripped in the regex but there has to be a

regular expression to detect numbers written as words - UTF-8 input

落爺英雄遲暮 提交于 2019-12-10 10:31:22
问题 thanks for the answers to : "regular expression to detect numbers written as words" : regular expression to detect numbers written as words I now have this working, however I have the same requirement but the numbers as words are in Arabic (or any other UTF-8) and not English, so : if (preg_match("/\p{L}\b(?:(?:واحد|اثنان|ثلاثة|أربعة|خمسة|ستة|سبعة|ثمانية|تسعة|صفر|عشرة)\b\s*?){4}/", $str, $matches) > 0) return true; Does not work - I've googled and there seems to be quite a few issues with

replacing all image src tags in HTML text

只谈情不闲聊 提交于 2019-12-09 08:33:29
I'm trying to make a simple php script to find all src attributes from all images in a html text and then replace all found srcs with some text after making some conditional changes. Something like this: @preg_match_all('/<img\s src="([a-zA-Z0-9\.;:\/\?&=_|\r|\n]{1,})"/isxmU', $body, $images); now i've all srcs into the $images variable, now i make: foreach ($images as $img) { ..my changes here.. } and now... how can i restore the changed srcs to the $body variable again?? many thanks in advance, Use a HTML DOM parser instead, much easier to use and maintain http://simplehtmldom.sourceforge

PHP regex non-capture non-match group

£可爱£侵袭症+ 提交于 2019-12-09 05:10:59
问题 I'm making a date matching regex, and it's all going pretty well, I've got this so far: "/(?:[0-3])?[0-9]-(?:[0-1])?[0-9]-(?:20)[0-1][0-9]/" It will (hopefully) match single or double digit days and months, and double or quadruple digit years in the 21st century. A few trials and errors have gotten me this far. But, I've got two simple questions regarding these results: (?: ) what is a simple explanation for this? Apparently it's a non-matching group. But then... What is the trailing ? for? e

Regex for finding valid filename

天大地大妈咪最大 提交于 2019-12-09 04:23:08
问题 I want to check whether a string is a file name (name DOT ext) or not. Name of file cannot contain / ? * : ; { } \ Could you please suggest me the regex expression to use in preg_match()? 回答1: Here you go: "[^/?*:;{}\\]+\\.[^/?*:;{}\\]+" "One or more characters that aren't any of these ones, then a dot, then some more characters that aren't these ones." (As long as you're sure that the dot is really required - if not, it's simply: "[^/?*:;{}\\]+" 回答2: $a = preg_match('=^[^/?*;:{}\\\\]+\.[^/?*

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

烈酒焚心 提交于 2019-12-09 02:51:44
问题 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

Find a percentage value in a string using preg_match

家住魔仙堡 提交于 2019-12-09 00:03:28
问题 I'm trying to isolate the percentage value in a string of text. This should be pretty easy using preg_match, but because the percentage sign is used as an operator in preg_match I can't find any sample code by searching. $string = 'I want to get the 10% out of this string'; What I want to end up with is: $percentage = '10%'; My guess is that I'll need something like: $percentage_match = preg_match("/[0-99]%/", $string); I'm sure there is a very quick answer to this, but the solution is

how to do a preg_replace in twig

人走茶凉 提交于 2019-12-08 17:24:48
问题 I currently am trying to make a variable using the current url for the view. To get the url, I am using {% set uri = app.request.uri %} (uri being my variable which is the current url for that particular view). The thing is, I am only interested in what makes the url unique (the end of it - a unique object from an array - happens to be a uri), and not the beginning (path to my application). I was thinking I could use a preg_replace to do so, but TWIG doesn't have this function. Just wondering

PHP: string to regex

依然范特西╮ 提交于 2019-12-08 15:39:18
问题 I try use string as a regular expression pattern but I've following errors PHP Warning: preg_match(): Unknown modifier '>' in /Applications/MAMP/htdocs/cruncher/Plugins/wordpress/WPDetect.php on line 22 PHP Warning: preg_match(): Unknown modifier '/' in /Applications/MAMP/htdocs/cruncher/Plugins/wordpress/WPDetect.php on line 22 The code $str = "<meta name=\"generator\" content=\"WordPress.com\" />" preg_match("/".$str."/", $content->content) I also tried to use preg_quote function but I've