How to use regex to delete everything except some words? [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Regular expression: match all words except
I need your help for using Regex in PHP to negate a selection. So I have a string like this :
"Hello my name is tom"
What I need to do is to delete everything from this string witch is not "tom" or "jack" or "alex" so I tried :
$MyString = "Hello my name is tom"
print_r(preg_replace('#^tom|^jack|^alex#i', '', $MyString));
But it's not working...
Can you help me with that ?
Thanks

If you want to delete everything except something, may be it's better done the other way around: capture the something only? For example...
$testString = 'Hello my name is tom or jack';
$matches = array();
preg_match_all('/\b(tom|jack|alex)\b/i', $testString, $matches);
$result = implode('', $matches[0]);
echo $result; // tomjack
What you've tried to do is use a character class syntax ([^s] will match any character but s). But this doesn't work with series of characters, there's no such thing as 'word class'. )

If you want to remove everything that is not "tom" or "jack" or "alex" you can use the following:
$MyString = "Hello my name is jack";
print_r(preg_replace('#.*(tom|jack|alex)#i', '$1', $MyString));
This replaces the whole string with just the matched name.

regex:
\b(?!tom|jack|alex)[^\s]+\b

You could match what you want and then reconstruct the string:
$s = 'hello my name is tom, jack and alex';
if (preg_match_all('/(?:tom|jack|alex)/', $s, $matches)) {
print_r($matches);
$s = join('', $matches[0]);
} else {
$s = '';
}
echo $s;
Output:
tomjackalex

Related

Php get specific word of string

What is the php function to extract only the word Duitsland from the following string /Duitsland|/groepsreizen fietsen|Stars/1.
I tried everything but dit not find the right method.
http://php.net/manual/de/function.str-replace.php
$result= str_replace("Duitsland", "", "/Duitsland|/groepsreizen fietsen|Stars/1");
Result: "/|/groepsreizen fietsen|Stars/1"
There is multiple way to do this work. One way is using regex. Use regex in preg_match() to finding specific word of string.
$str = "/Duitsland|/groepsreizen fietsen|Stars/1";
preg_match("/[^|\/]+/", $str, $match);
echo $match[0];
You can test it in demo
with strpos function find require o/p
$haystack = '/Duitsland|/groepsreizen fietsen|Stars/1';
$needle = 'Duitsland';
if (strpos($haystack,$needle) !== false) {
echo $needle;
}
I think this is what you're looking for:
(this code does not need to know what is the word before, it just seeks the first (as long as possible) word in the string)
<?php
$str = "/Duitsland|/groepsreizen fietsen|Stars/1";
preg_match("/\w+/i", $str, $matches);
$first_word = array_shift($matches);
echo $first_word;
It will work no matter how many non-letter symbols there are before that word, i.e. it's not dependent on any fixed-count other characters.
Demo: http://ideone.com/GX8IT6

PHP preg_match getting string between 2 chars

Please consider this string:
$string = 'hello world /foo bar/';
The end result I wish to obtain:
$result1 = 'hello world';
$result2 = 'foo bar';
What I've tried:
preg_match('/\/(.*?)\//', $string, $match);
Trouble is this only return "foo bar" and not "hello world". I can probably strip "/foo bar/" from the original string, but in my real use case that would take additional 2 steps.
The regular expression only matches what you tell it to match. So you need to have it match everything including the /s and then group the /s.
This should do it:
$string = 'hello world /foo bar/';
preg_match('~(.+?)\h*/(.*?)/~', $string, $match);
print_r($match);
PHP Demo: https://eval.in/507636
Regex101: https://regex101.com/r/oL5sX9/1 (delimiters escaped, in PHP usage changed the delimiter)
The 0 index is everything found, 1 the first group, 2 the second group. So between the /s is $match[2]; the hello world is $match[1]. The \h is any horizontal whitespace before the / if you want that in the first group remove the \h*. The . will account for whitespace (excluding new line unless specified with s modifier).
$result = explode("/", $string);
results in
$result[0] == 'hello world ';
$result[1] == 'foo bar';
You might want to replace the space in hello world. More info here: http://php.net/manual/de/function.explode.php
To solve this conversion issue, use below code.
$string = 'hello world /foo bar/';
$returnValue = str_replace(' /', '/', $string);
$result = explode("/", $returnValue);
If you want to print it, put below lines in your code.
echo $pieces[0]; // hello world
echo $pieces[1]; // foo bar
https://eval.in/507650

Why is my regex rejecting apostrophes?

I'm making a regex which should match everything like that : [[First example]] or [[I'm an example]].
Unfortunately, it doesn't match [[I'm an example]] because of the apostrophe.
Here it is :
preg_replace_callback('/\[\[([^?"`*%#\\\\:<>]+)\]\]/iU', ...)
Simple apostrophes (') are allowed so I really do not understand why it doesn't work.
Any ideas ?
EDIT : Here is what's happening before I'm using this regex
// This match something [[[like this]]]
$contents = preg_replace_callback('/\[\[\[(.+)\]\]\]/isU',function($matches) {
return '<blockquote>'.$matches[1].'</blockquote>';
}, $contents);
// This match something [[like that]] but doesn't work with apostrophe/quote when
// the first preg_replace_callback has done his job
$contents = preg_replace_callback('/\[\[([^?"`*%#\\\\:<>]+)\]\]/iU', ..., $contents);
try this:
$string = '[[First example]]';
$pattern = '/\[\[(.*?)\]\]/';
preg_match ( $pattern, $string, $matchs );
var_dump ( $matchs );
You can use this regex:
\[\[.*?]]
Working demo
Php code
$re = '/\[\[.*?]]/';
$str = "not match this but [[Match this example]] and not this";
preg_match_all($re, $str, $matches);
Btw, if you want to capture the content within brackets you have to use capturing groups:
\[\[(.*?)]]

Split string at the first non-alphabetic character

How can I get a portion of the string from the beginning until the first non-alphabetic character?
Example strings:
Hello World
Hello&World
Hello5World
I'd like to get the "Hello" part
You need to use the preg_split feature.
$str = 'Hello&World';
$words = preg_split('/[^\w]/',$str);
echo $words[0];
You can access Hello by $words[0], and World by $words[1]
You can use preg_match() for this:
if (preg_match('/^([\w]+)/i', $string, $match)) {
echo "The matched word is {$match[1]}.";
}
Change [\w]+ to [a-z]+ if you do not want to match the 5 or any numeric characters.
Use preg_split. Split string by a regular expression
If you only want the first part, use preg_match:
preg_match('/^[a-z]+/i', $str, $matches);
echo $matches[0];
Here's a demo.
Use preg_split to get first part of alpha.
$array = preg_split('/[^[:alpha:]]+/', 'Hello5World');
echo $array[0];

Why is this preg_split not working as I want it to?

I have a preg_split pattern as below:
$pattern = '/[, ;:-_.|+#\/]/';
I use
$pcs = preg_split($pattern, $string);
if string is " Hello how are you", then count($pcs) == 5.
if string is "Hello how are you", then count($pcs) == 4.
if string is " Hello how are you ", then count($pcs) == 6.
Does anybody know what the problem is? (I want it to return 4 in all above cases).
My plan is to split a user-inputted string into words simply.
The string entered may contain the characters in the pattern above.
Thanks
Try
$pcs = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY);
Read more in the manual: http://php.net/manual/en/function.preg-split.php
Look at your whitespace. I am going to assume the following:
"Hello how are you" split results has $pcs[0] == ''
"Hello how are you " split results has $pcs[0] == '' and $pcs[5] == ''
Since there is white space at the beginning and/or end (depending on the case) your preg_split is going to still create the split between nothing and the first and/or last word. If you want to avoid this you should run a trim() first before your split.

Categories