Remove all before specific symbol in string PHP - php

For example i have string one two three four five and I want remove all characters before two and after four , I know that is function preg_replace() but I don't know how to write this expression, I not know what does mean for example '/([a-z])([A-Z])/' please say what is name of this expression in $pattern and what they mean

In case you're looking for preg_replace based solution then here it is:
$str = 'one two three four five';
var_dump ( preg_replace('#^.*?(two.*?four).*$#i', '$1', $str) );
Expanation: RegEx used in preg_replace first matches text upto your starting text two then matches upto your end text four and finally replaces it with matched string thus discarding all text before two and all text after four. Please note .*? makes your matching non-greedy. Read more about regex here: http://www.regular-expressions.info/
OUTPUT
string(14) "two three four"

preg_replace is a function which takes in regular expressions to do replacements.
You can and should learn about these, as they are very powerful, but they aren't essential for your problem.
You can use strpos and substr functions
substr takes a string that you want to shorten, a start location and a number of characters and returns the shortened string.
strpos takes a string that you want to search, and a string that you want to search for and returns the location of the second string in the first.
So you can use them as such:
$text = "one two three four five";
$locationOfTwo = strpos($text, "two"); //The location in the string of the substring "two" (in this case it's 4)
$locationOfFour =strpos($text, "four") + strlen("four"); //Added the length of the word to get to the end of it (In this case it will set the variable to 18).
$subString = subsstr($text, locationOfTwo, (locationOfFour-locationOfTwo));

Related

preg_replace - similar patterns

I have a string that contains something like "LAB_FF, LAB_FF12" and I'm trying to use preg_replace to look for both patterns and replace them with different strings using a pattern match of;
/LAB_[0-9A-F]{2}|LAB_[0-9A-F]{4}/
So input would be
LAB_FF, LAB_FF12
and the output would need to be
DAB_FF, HAD_FF12
Problem is, for the second string, it interprets it as "LAB_FF" instead of "LAB_FF12" and so the output is
DAB_FF, DAB_FF
I've tried splitting the input line out using 2 different preg_match statements, the first looking for the {2} pattern and the second looking for the {4} pattern. This sort of works in that I can get the correct output into 2 separate strings but then can't combine the two strings to give the single amended output.
\b is word boundary. Meaning it will look at where the word ends and not only pattern match.
https://regex101.com/r/upY0gn/1
$pattern = "/\bLAB_[0-9A-F]{2}\b|\bLAB_[0-9A-F]{4}\b/";
Seeing the comment on the other answer about how to replace the string.
This is one way.
The pattern will create empty entries in the output array for each pattern that fails.
In this case one (the first).
Then it's just a matter of substr.
$re = '/(\bLAB_[0-9A-F]{2}\b)|(\bLAB_[0-9A-F]{4}\b)/';
$str = 'LAB_FF12';
preg_match($re, $str, $matches);
var_dump($matches);
$substitutes = ["", "DAB", "HAD"];
For($i=1; $i<count($matches); $i++){
If($matches[$i] != ""){
$result = $substitutes[$i] . substr($matches[$i],3);
Break;
}
}
Echo $result;
https://3v4l.org/gRvHv
You can specify exact amounts in one set of curly braces, e.g. `{2,4}.
Just tested this and seems to work:
/LAB_[0-9A-F]{2,4}/
LAB_FF, LAB_FFF, LAB_FFFF
EDIT: My mistake, that actually matches between 2 and 4. If you change the order of your selections it matches the first it comes to, e.g.
/LAB_([0-9A-F]{4}|[0-9A-F]{2})/
LAB_FF, LAB_FFFF
EDIT2: The following will match LAB_even_amount_of_characters:
/LAB_([0-9A-F]{2})+/
LAB_FF, LAB_FFFF, LAB_FFFFFF...

Move multiple letters in string using regex

Using a regular expression I want to move two letters in a string.
W28
L36
W29-L32
Should be changed to:
28W
36L
29W-32L
The numbers vary between 25 and 44. The letters that need to be moved are always "W" and/or "L" and the "W" is always first when they both exist in the string.
I need to do this with a single regular expression using PHP. Any ideas would be awesome!
EDIT:
I'm new to regular expressions and tried a lot of things without success. The closest I came was using "/\b(W34)\b/" for each possibility. I also found something about using variables in the replace function but had no luck using these.
Your regex \b(W34)\b matches exactly W34 as a whole word. You need a character class to match W or L, and some alternatives to match the numeric range, and use the most of capturing groups.
You can use the following regex replacement:
$re = '/\b([WL])(2[5-9]|3[0-9]|4[0-4])\b/';
$str = "W28\nL36\nW29-L32";
$result = preg_replace($re, "$2$1", $str);
echo $result;
See IDEONE demo
Here, ([WL]) matches and captures either W or L into group 1, and (2[5-9]|3[0-9]|4[0-4]) matches integer numbers from 25 till 44 and captures into group 2. Backreferences are used to reverse the order of the groups in the replacement string.
And here is a regex demo in case you want to adjust it later.

regex to match string that only has one uppercase letter

I am curling an page and getting the output
however what is happening is that the html encoding is being removed so new lines are being skipped,
so it looks like this
This is Bob. He lives in an boatBut he only has one oar to row with.
in order to detect new lines I figure it was easier to just check for strings that only have One upper case letter and spaces inbetween, so far I have this
(\s\w+\s\w+.\s\D+[a-z][A-Z])
However this does not seem to work
as it only matches this
is Bob. He lives in an boatB
see here http://regex101.com/r/gH0lW1
how to match all strings that have spaces and match all strings up to one Uppercase letter
Update: this will split on the condition without losing any characters
<?php
$string = "This is Bob. He lives in an boatBut he only has one oar to row with.He also does stuff, it is cool.";
$array = preg_split('/(?<=[a-z.])(?=[A-Z])/', $string);
print_r($array);
?>
Use a positive lookbehind to ensure you capture a capital after a lowercase:
(?<=[a-z])[A-Z]
http://regex101.com/r/cB7bD8
You could use php's preg_split if you want, to explode the result on this regex.
(.*?(?:\w+(?=[A-Z]))|\1)
This regex has a recursive part that will match more than 1 sentence in a whole text. So you can check the Live demo and see the matched groups.
But,
If you wanna include a newline on each sentence begins after a period (.) as well, then I modify above regex to this:
(.*?(?:(?:\w+|\. *)(?=[A-Z]))|\1)
and now you can compare results with the first regex HERE

pregmatch between characters and any numeric

I'm stuck writing a preg_match
I have a string:
XPMG_ar121023.txt
and need to extract the 2 letters between XPMG_ and the first digit - be it a 0-9
$str = 'XPMG_ar121023.txt';
preg_match('/('XPMG_')|[0-9\,]))/', $str, $match);
print_r($match);
Maybe this isn't the best option: My characters will always be
You can just do
$str = "XPMG_ar121023.txt" ;
preg_match('/_([a-z]+)/i', $str, $match);
var_dump($match[1]);
Output
string 'ar' (length=2)
This is too simple for a regular expression. Just $match = substr($str,5,3) would get what you're asking for.
Let me walk through this step by step so as to help you solve similar problems in the future. Suppose we have the following format for our filenames:
XPMG_ar121023.txt
We know what we want to capture, we want the "ar" right after the _ and just before the numbers begin. So our expression would look something like this:
_[a-z]+
This is pretty straight-forward. We're starting by looking for an underscore, followed by any number of letters between a and z. The square brackets define a character class. Our class consists of the alphabet, but you can push specific numbers in there and more if you like.
Now because we want to capture only the letters, we need to put parenthesis around that part of the pattern:
_([a-z]+)
In the result we will now have access to only that subpattern. Next we put our delimiters in place to specify where our pattern begins, and ends:
/_([a-z]+)/
And lastly, after our closing delimiter we can add some modifiers. As it is written, our pattern only looks for lower-case letters. We can add the i modifier to make this case-insensitive:
/_([a-z]+)/i
Voila, we're done. Now we can pass it into preg_match to see what it spits out:
preg_match( "/_([a-z]+)/i", "XPMG_ar121023.txt", $match );
This function takes a pattern as the first parameter, a string to match it against as the second, and lastly a variable to spit the results into. When all is said and done, we can check $match for our data.
The results of this operation follow:
array(2) {
[0]=> string(3) "_ar"
[1]=> string(2) "ar"
}
This is the contents of $match. Notice our full pattern is found in the first index of the array, and our captured portion is provided in the second index of the array.
echo $match[1]; // ar
Hope this helps.
Well, why not:
$letters = $str[5].$str[6];
:)
After all, you'll always need the 2 chars after the fixed prefix, there are many ways that do not require a regexp (substr() being the best anyway)

help with regular expression

is it possible to get a regular expression that do:
find first occurence of some word in a string
return a substring of a nr of letters before and after the occurence
but if it encounters a . (dot) before the nr of letters before and after occurence, it will just return the sub string before/after the dot.
return whole words
example:
"Anyone who knows how to do this. Create a program that inputs a regular expression and outputs strings that satisfy that regular expression. And bla bla"
if the keyword is 'program' and we put nr of letters to 20 it will return 20 letters before and after 'program'. But since it encounters a dot before it gets to 20 letters it will stop there.
"Create a program that inputs a regular..."
Is this possible with regexp? what php function do i have to use? is there any finnished script for this? I guess its a quite basic need when showing search results. Someone already got the function to share?
Here's Dav's regular expression in php:
<?php
$str = "Anyone who knows how to do this. Create a program that inputs a regular expression and outputs strings that satisfy that regular expression. And bla bla";
$key = "program";
$lim = 20;
$reg = "/([^.]{0,{$lim}})({$key})([^.]{0,{$lim}})/"; // /[^.]{0,20}program[^.]{0,20}/
$res = preg_match($reg, $str, $matches);
echo $matches[0];
print_r($matches); // $matches[1] is the pre-text, and $matches[3] is the post-text
The trickiest requirement is #4: "return whole words". One way you could handle this while still making use of the above regular expression is pull more text before and after than you really want (say 40 chars). Then you could preg_split the before and after text on whitespace, which would give you two arrays of words. Run the arrays through a function that gives you back a subset of the array where the total length of all the words is less than your limit of 20...
[^.]{0,MAXCHARS}wordtofind[^.]{0,MAXCHARS}
Replace MAXCHARS with the number corresponding to the maximum number of characters you want on each side.
The [^.] pattern matches any character that's not a period, and the {0,MAXCHARS} qualifier matches anywhere from 0 to MAXCHARS of those characters.

Categories