preg-match

How to extract only part of string in PHP?

心不动则不痛 提交于 2019-12-31 03:29:21
问题 I have a following string and I want to extract image123.jpg. ..here_can_be_any_length "and_here_any_length/image123.jpg" and_here_also_any_length image123 can be any length (newimage123456 etc) and with extension of jpg, jpeg, gif or png. I assume I need to use preg_match, but I am not really sure and like to know how to code it or if there are any other ways or function I can use. 回答1: You can use: if(preg_match('#".*?\/(.*?)"#',$str,$matches)) { $filename = $matches[1]; } Alternatively you

Regex for getting information from square brackets

家住魔仙堡 提交于 2019-12-31 03:12:09
问题 I'm attempting to make something like shortcodes in WordPress, and I'm looking for a regex that will take something like [title:This is a title] and turn it to just This is a title . It would also be useful if someone could suggest a way to take something like [code:some code] and turn that into an array in the form of array( [0] => 'code' [1] => 'some code') or array( 'code' => 'some code' I've tried a few different regexes I've found here and there, but none of them seem to work. Thanks 回答1

regular expression with hebrew or english

牧云@^-^@ 提交于 2019-12-31 03:08:28
问题 I'm looking for a pattern that accept only hebrew or english letters from 2 letters to 15, and can accept 1 space. I have tried following code but it does not matching my string: <?php $subject = "שלום לך"; $regexp="#^\p[{Hebrew}| ][a-zA-Z]{2,15}? \+$#u"; print_r(preg_match($regexp, $subject)); ?> 回答1: There are multiple errors in your code. First, your regex $regexp="#^\p[{Hebrew}| ][a-zA-Z]{2,15}? \+$#u"; Here is what it means: # : regex delimiter ^ : begining of string \p : character p [

PHP function to grab all links inside a <DIV> on remote site using scrape method

亡梦爱人 提交于 2019-12-30 07:18:45
问题 Anyone has a PHP function that can grab all links inside a specific DIV on a remote site? So usage might be: $links = grab_links($url,$divname); And return an array I can use. Grabbing links I can figure out but not sure how to make it only do it within a specific div. Thanks! Scott 回答1: Check out PHP XPath. It will let you query a document for the contents of specific tags and so on. The example on the php site is pretty straightforward: http://php.net/manual/en/simplexmlelement.xpath.php

Detecting emails in a text

一世执手 提交于 2019-12-30 02:53:05
问题 I'm trying to create a function that translates every occurrence of a plain text email address in a given string into it's htmlized version. Let's say I have the following code, where htmlizeEmails is the function I'm looking for: $str = "Send me an email to bob@example.com."; echo htmlizeEmails($str); // Echoes "Send me an email to <a href="mailto:bob@example.com">bob@example.com</a>." If possible, I'd like this function to use the filter_var function to check if the email is valid. Does

How to replace only the last match of a string with preg_replace?

爱⌒轻易说出口 提交于 2019-12-29 06:25:15
问题 I have to replace the last match of a string (for example the word foo) in HTML document. The problem is that the structure of the HTML document is always random. I'm trying to accomplish that with preg_replace, but so far I know how to replace only the first match, but not the last one. Thanks. 回答1: Use negative look after (?!...) $str = 'text abcd text text efgh'; echo preg_replace('~text(?!.*text)~', 'bar', $str),"\n"; output: text abcd text bar efgh 回答2: A common approach to match all

How to replace only the last match of a string with preg_replace?

浪子不回头ぞ 提交于 2019-12-29 06:25:11
问题 I have to replace the last match of a string (for example the word foo) in HTML document. The problem is that the structure of the HTML document is always random. I'm trying to accomplish that with preg_replace, but so far I know how to replace only the first match, but not the last one. Thanks. 回答1: Use negative look after (?!...) $str = 'text abcd text text efgh'; echo preg_replace('~text(?!.*text)~', 'bar', $str),"\n"; output: text abcd text bar efgh 回答2: A common approach to match all

Finding HTML tags in string

守給你的承諾、 提交于 2019-12-28 18:24:21
问题 I know this question is around SO, but I can't find the right one and I still suck in Regex :/ I have an string and that string is valid HTML. Now I want to find all the tags with an certain name and attribute . I tried this regex (i.e. div with type): /(<div type="my_special_type" src="(.*?)<\/div>)/ . Example string: <div>Do not match me</div> <div type="special_type" src="bla"> match me</div> <a>not me</a> <div src="blaw" type="special_type" > match me too</div> If I use preg_match then I

Matching a multiple lines pattern via PHP's preg_match()

纵饮孤独 提交于 2019-12-28 16:30:11
问题 How can I match subject via a PHP preg_match() regular expression pattern in this HTML code: <table border=0> <tr> <td> <h2>subject</h2> </td> All the whitespaces and newlines are left on purpose. So the problem is in extracting subject name using some multiple line pattern. 回答1: If you're looking for (e.g.) a h2 tag nested within a td tag where there's only whitespace in between the two, just use \s which includes spaces, newlines, etc. eg:: preg_match('#<td>\s*<h2>(.*?)</h2>\s*</td>#i',$str

Regular expression for allowed characters

放肆的年华 提交于 2019-12-25 18:45:38
问题 i need to check, if the string in my PHP code contains only a specified list of characters. But as some of them might need scape, i just can't get it to work. I have tried everything, but it just doesen't work... The required validation is: 0-9 A-Z a-z chars: .%^& ()$#@!/ -+/ Basicaly, i want to check if there is an unknown ascii character (i don't know if there is a function to do that already). My code is like this now: if(preg_match("/[A-Za-z0-9\.\#\-\+\*\\\/\=\_\%\$\(\)]/", $cmd) ===