i have a string $content = "I Love Digital Point so Much"; PHP: and want to do a match so i can get the "Digital", "Point" and the "Much" on the output. can i do an array on the pattern part of preg_match_all preg_match_all(array("/Digital/","/Point/","/Much/"), $content, $match); PHP: so i wont have to use preg match all always. since im using the code to find the different and many patterns found on a single string and so the output can be something like $match[1][0] outputs Digital $match[1][1] outputs Point $match[1][2] outputs Much TIA. need it for finals. T_T
If its a case of you have a list of unwanted words "I, love or so" you could do something like this: preg_replace() <?php $content = 'I Love Digital Point so Much'; $patterns = array(); $patterns[0] = '/I/'; $patterns[1] = '/Love/'; $patterns[2] = '/so/'; $replacements = array(); $replacements[2] = ''; $replacements[1] = ''; $replacements[0] = ''; echo preg_replace($patterns, $replacements, $content); ?> PHP: Which will replace those words with nothing thus the output is as requested: Digital Point Much Or maybe there's some function() you have to use, I assumed from your post you didn't want to you preg_match_all()
i need to MATCH "Digital", "Point" and "Much" from the string so i can use the matched strings in some other things. so replace is not an option.
OK how about this... basically we give our search pattern terms a name like <digital> and if we get a match its $matches['digital'] you can of course number these instead, also we are searching for both upper and lower case, just in case they try and catch you out. <?php $content = 'I Love Digital Point so Much'; $pattern = '#(?<digital>Digital|digital)(.*)(?<point>Point|point)(.*)(?<much>Much|much)#'; preg_match($pattern, $content, $matches); $digital = $matches['digital']; $point = $matches['point']; $much = $matches['much']; echo $digital; echo "<br />"; echo $point; echo "<br />"; echo $much; ?> PHP:
is it possible to do a regex for all the patterns? since i'm not just looking for a word but also regex... and from what i understand in the code, it's not possible to interchange the pattern.... let's say the string $content = "I Love Digital Point so Much very very Much digital Love"; PHP: so i need to get all the "Digital", "Point" and "Much". i can't just use this.... $pattern = '#(?<digital>Digital|digital)(.*)(?<point>Point|point)(.*)(?<much>Much|much)(.*)(?<much>Much|much)(.*)(?<much2>Much|much)(.*)(?<digital2>Digital|digital)#'; PHP: it would be a pain. XD
No your right, the code I provided means the words must come in the order Digital, Point and Much, I'm sorry but the scoop of your requirements is beyond my general knowledge, maybe someone else will participate in the thread. At a glance its going to take a lot of time for someone to code this for you, you could try the programming section here on DP or if your interested in a recommendation try a PM to @danx10 for a quote on the job.
<?php $content = 'I Love Digital Point so Much'; preg_match_all('~\b([A-Z][a-z]+)\b~', $content, $a); echo '<pre>'; print_r($a); echo '</pre>'; ?> PHP:
I think he wants to take all the matching words "digital point and much" from a random string ie: $content = "I Love Digital Point so Much very very Much digital Love"; PHP: ..and assign those individually to a variable for future use within his script. So from your code output how does he check against the array results to get the corresponding word match result ? Would he need to do a lot of if statements == digital ? Array ( [0] => Array ( [0] => Love [1] => Digital [2] => Point [3] => Much ) [1] => Array ( [0] => Love [1] => Digital [2] => Point [3] => Much ) ) Code (markup):
Thats what the code does: $a is an array, theirfore you can pick/select/output specic values like so: <?php $content = 'I Love Digital Point so Much'; preg_match_all('~\b([A-Z][a-z]+)\b~', $content, $a); /* would output Love */ echo $a[0][0]; /* would output Digital */ echo $a[0][1]; //etc.. ?> PHP: I believe all the OP wants is the be able to output them (not validate/compare theirfore no need to use if statements)
Yeah, that was his first statement until he said he wanted all instances ie its going to be a random $string so we don't know what echo $a[0][0]; is going to be, maybe I am misunderstanding what the op wants. However here's something that will out-put the first 5 instances of either word, hopefully 5 will be enough? each instance is assigned like $digital[0][0], $point[0][2] etc.. as to how you find out if you have an instance or not, I have no idea. <?php $content = "I Much Digital Love much digital point point Digital love Point so Much point very love point very Much digital Love"; function get_digital($regex,$content) { preg_match_all($regex,$content,$Digital); return $Digital[0][0] . ' ' . $Digital[0][1] . ' ' . $Digital[0][2] . ' ' . $Digital[0][3] . ' ' . $Digital[0][4]; } function get_point($regex,$content) { preg_match_all($regex,$content,$Point); return $Point[0][0] . ' ' . $Point[0][1] . ' ' . $Point[0][2] . ' ' . $Point[0][3] . ' ' . $Point[0][4]; } function get_much($regex,$content) { preg_match_all($regex,$content,$Much); return $Much[0][0] . ' ' . $Much[0][1] . ' ' . $Much[0][2] . ' ' . $Much[0][3] . ' ' . $Much[0][4]; } $digital = get_digital('#(Digital|digital)#',$content); $point = get_point('#(Point|point)#',$content); $much = get_much('#(Much|much)#',$content); echo $digital; echo "<br />"; echo $point; echo "<br />"; echo $much; ?> PHP: PS: please remember I'm a newbie so no laughing
The OP meant the $content will be a random string, but the concept will be the same no matter how the format of the random string is...if it follows the pattern of the regex (as its generic) it will match and the matches will be contained within the array. In that array the OP can loop through to display its contents/values...or print_r() to see specific values and then do whatever the OP'd like with them. Furthermore: $digital = get_digital('#(Digital|digital)#',$content); PHP: This can be achieved in a simpler form: $digital = get_digital('#(digital)#i',$content); PHP: By add the i modifier to the end of the expression, which will make it case insensitive, theirfore wouldn't matter what case
This could be the function you're after, but doesn't use regular expressions. Useful to extract parts of a string from a string. function return_strings_between($text, $openingMarker, $closingMarker) { $openingMarkerLength = strlen($openingMarker); $closingMarkerLength = strlen($closingMarker); $result = array(); $position = 0; while (($position = strpos($text, $openingMarker, $position)) !== false) { $position += $openingMarkerLength; if (($closingMarkerPosition = strpos($text, $closingMarker, $position)) !== false) { $result[] = substr($text, $position, $closingMarkerPosition - $position); $position = $closingMarkerPosition + $closingMarkerLength; } } return $result; } PHP:
This should work: $content = "I Love Digital Point so Much very very Much digital Love"; $wordList = array('digital','point','much'); foreach($wordList as $word) { preg_match_all('/.*('.$word.').*/Ui',$content,$result); print_r($result[1]); } PHP: A bit long, but that will work no matter how many number of digital or point there are.