preg-match

How to get span tag content using preg_match function? [closed]

谁说胖子不能爱 提交于 2019-12-02 06:44:42
I have the following HTML code: <span role="button" class="a-n S1xjN" tabindex="0">414,817 people</span> How to use preg_match function to get the 414817 number? Amal Murali The Better Solution A regex is the wrong tool here. HTML is not a regular language , and cannot be accurately parsed using regular expressions. Use a DOM parser instead. Not only is it much easier, it's more accurate and reliable, and won't break when the format of the markup changes in the future. This is how you would get the contents inside a <span> tag using PHP's built-in DOMDocument class: $dom = new DOMDocument;

Warning: preg_match() [function.preg-match]: Unknown modifier [duplicate]

丶灬走出姿态 提交于 2019-12-02 05:52:24
问题 This question already has answers here : Warning: preg_replace(): Unknown modifier ']' (3 answers) Closed 5 months ago . I'm getting this error... Warning: preg_match() [function.preg-match]: Unknown modifier '1' in C:\path-to-plugin.php on line 147 When I run the keyword "Test $2/1 test+word!" through the function below function my_get_kw_in_content($theKeyword, $theContent) { //ERROR OCCURS NEXT LINE return preg_match('/\b' . $theKeyword . '\b/i', $theContent); } I'm assuming that I need to

php preg_match and ereg syntax difference

家住魔仙堡 提交于 2019-12-02 05:11:26
I found that syntax of preg_match() and the deprecated ereg() is different. For example: I thought that preg_match('/^<div>(.*)</div>$/', $content); means the same as ereg('^<div>(.*)</div>$', $content); but I was wrong. preg_match() doesn't include special characters as enter like ereg() does. So I started to use this syntax: preg_match('/^<div>([^<]*)</div>$/', $content); but it isn't exactly the same to what I need. Can anyone suggest me how to solve this problem, without using deprecated functions? CSᵠ For parsing HTML I'd suggest reading this question and choosing a built in PHP extension

preg_match -ing from lines in a .txt file (with spaces and brackets!)

雨燕双飞 提交于 2019-12-02 04:47:49
So I have got this far on my own but It looks like I have found the limit of my PHP knowledge (which isn't very much at all!). This script is for filtering filenames (game roms/iso's etc). It has other ways of filtering too but I've just highlighted the section I'm trying to add. I want a external .txt file I can put names of files in like so (separated by a single line break): Pacman 2 (USA) Space Invaders (USA) Asteroids (USA) Something Else (Europe) And then running the script will search the directory and place any matching filenames in the "removed" folder. It loops fine with all the

preg_match all paragraphs in a string

我只是一个虾纸丫 提交于 2019-12-02 04:09:22
The following string contains multiple <p> tags. I want to match the contents of each of the <p> with a pattern, and if it matches, I want to add a css class to that specific paragraph. For example in the following string, only the second paragraph content matches, so i want to add a class to that paragraph only. $string = '<p>para 1</p><p>نص عربي أو فارسي</p><p>para3</p>'; With the following code, I can match all of the string, but I am unable to figure out how to find the specific paragraph. $rtl_chars_pattern = '/[\x{0590}-\x{05ff}\x{0600}-\x{06ff}]/u'; $return = preg_match($rtl_chars

get wrapping element using preg_match php

梦想的初衷 提交于 2019-12-02 03:58:20
I want a preg_match code that will detect a given string and get its wrapping element. I have a string and a html code like: $string = "My text"; $html = "<div><p class='text'>My text</p><span>My text</span></div>"; So i need to create a function that will return the element wrapping the string like: $element = get_wrapper($string, $html); function get_wrapper($str, $code){ //code here that has preg_match and return the wrapper element } The returned value will be array since it has 2 possible returning values which are <p class='text'></p> and <span></span> Anyone can give me a regex pattern

How to remove content from url after question mark. preg_match or preg_replace?

风流意气都作罢 提交于 2019-12-02 03:56:30
问题 Im working on some function to grab few data from url with simple html dom. But one of the data is an image and image have question mark and some more info behind it. So example of url would be something like this. http://somesite.com/uploaded/images/8.jpg?m=eSuQKgaaaa&mh=t0i7nVhjZleTJ5Ih So this content behind question mark is some sort of the code that makes images resized to small size. If i would just take this http://somesite.com/uploaded/images/8.jpg Images would be in bigger resolution

php preg_match, matching when 2 words might come in random sequence

天涯浪子 提交于 2019-12-02 03:50:25
Is it possible to match two words that might come in random sequences? Examples: $title = "2 pcs watch for couple"; $title = "couple watch 2 pcs"; Currently I'm using two regexes: if (preg_match("/2( )?pcs/i", $title) and preg_match("/couple/i", $title)) Just want to know if it can be done with only 1? If you're just testing for the presence of the two words in the string you could use '/couple.*2 pcs|2 pcs.*couple/' use strpos() if(strpos($string, '2 pcs') !== False){ //its found } or matching at first and at end if(preg_match("/(^(2 pcs|couples)|(2 pcs|couples)$)/", '2 pcs watch for couples'

PHP switch statement with preg_match

可紊 提交于 2019-12-02 03:28:42
问题 I have some problem to create a preg_match() inside my switch statement. I want to write preg_match that match /oop/page/view/[some-number]. For now its working like: If I run in my browser http://example.com/oop/page/view/1 its shows '404 page'. And when I run some address for example http://example.com/oop/page/view/test or even /oop/test its run 2nd case and dont know yet how. For sure something is wrong in my regex expresion.. public function check(){ $url = filter_input(INPUT_GET, 'url')

PHP switch statement with preg_match

别来无恙 提交于 2019-12-02 03:23:10
I have some problem to create a preg_match() inside my switch statement. I want to write preg_match that match /oop/page/view/[some-number]. For now its working like: If I run in my browser http://example.com/oop/page/view/1 its shows '404 page'. And when I run some address for example http://example.com/oop/page/view/test or even /oop/test its run 2nd case and dont know yet how. For sure something is wrong in my regex expresion.. public function check(){ $url = filter_input(INPUT_GET, 'url'); switch ($url) { case '': echo 'HomePage'; break; case preg_match('#^/oop/page/view/\d+$#', $url):