preg-match

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

元气小坏坏 提交于 2019-12-02 09:09:55
问题 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? 回答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/' 回答2: use strpos() if(strpos($string, '2 pcs') !== False){ //its found }

Adding dollar sign before each string in array?

雨燕双飞 提交于 2019-12-02 09:03:59
I have this string: $str = "(he+is+genius*2)/clever"; which looks like this in array; Array ( [0] => ( [1] => he [2] => + [3] => is [4] => + [5] => genius [6] => ) [7] => * [8] => and [9] => / [10] => clever ) What I want to do is placing dollar sign $ before each string present in $str but ignoring non-alphanumeric and numbers. At the end i want to have something which looks like this; $newstr = "($he+$is+$genius*2)/$clever"; For each value, check if the first char (or the whole value) is made of characters with ctype_alpha , then prepend with $ : // $arr is your array as defined in your

preg_match all paragraphs in a string

亡梦爱人 提交于 2019-12-02 08:54:41
问题 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

preg_replace within the preg_replace

ε祈祈猫儿з 提交于 2019-12-02 08:27:35
Right now I'm having issues replacing strings that already come out from preg_match. Lets say I have bbcode of [b]bla[/b] , I have this part working with replacing [b] with <b> , but lets just say for all testing purposes that they did [b]hi [b]test[/b][/b] , what ends up coming out is " hi [b]test[/b] ", with everything being bolded, but the [b] won't get replaced for some reason. Currently this is my expression: /\[b\](.*)\[\/b\]/ Sorry, I didn't show my code, I'm new to this. // Will convert string data into readable data function ConvertStringData2ReadableData($UglyString) { $CheckArrays =

get wrapping element using preg_match php

跟風遠走 提交于 2019-12-02 08:16:21
问题 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

PHP PCRE error preg_replace

风流意气都作罢 提交于 2019-12-02 08:09:19
问题 <?php function pregForPreg($value) { $value = preg_replace(array('#\(#', '#\)#', '#\+#', '#\?#', '#\*#', '#\##', '#\[#', '#\]#', '#\&#', '#\/#', '#\$#', '#\\\\#'), array('\(', '\)', '\+', '\?', '\*', '\#', '\[', '\]', '\&', '\/', '\\\$', '\\\\'), $value); return $value; } $var = "TI - Yeah U Know [OFFCIAL VIDEO] [TAKERS] [w\LYRICS]"; $var = pregForPreg($var); //$var is now: // TI - Yeah U Know \[OFFCIAL VIDEO\] \[TAKERS\] \[w\LYRICS\] $var = preg_replace("#" . $var . "#isU", 'test', $var);

php preg_match and ereg syntax difference

纵饮孤独 提交于 2019-12-02 08:09:08
问题 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

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

守給你的承諾、 提交于 2019-12-02 08:06:05
问题 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

Regex to match 2 or more words

拜拜、爱过 提交于 2019-12-02 07:27:52
I have a regex that tries to match for 2 or more words, but it isn't working as it's suppose to. What am I doing wrong? $string = "i dont know , do you know?"; preg_match("~([a-z']+\b){2,}~", $string, $match); echo "<pre>"; print_r($match); echo "</pre>"; Expected Result: Array ( i dont know ) Actual Result: Array ( ) This will match for string that contains 2 words or more exactly: /([a-zA-Z]+\s?\b){2,}/g you can go http://www.regexr.com/ and test it PHP: $string = "i dont know , do you know?"; preg_match("/([a-zA-Z]+\s?\b){2,}/", $string, $match); echo "<pre>"; print_r($match); echo "</pre>"

Php parse html dom and count specific rows

一个人想着一个人 提交于 2019-12-02 06:48:29
问题 I'm using the " Simple php DOM Parser " to parse an html table and count its row. I solved to count all the rows (tr) in it with this code: $rows = $table->find('.trClass'); $count = count($rows); echo $count; And I correctly get the number of all the rows in the table. Now I want to count only the rows which contains a specific td (with a specific string). We could assume that I want to count only the rows with this td: <td class="tdClass" align="center" nowrap="">TARGET STRING</td> How can