preg_split url with a regular expression - php

I have an string e.g.:
src="http://www.domain.com/sub_folder/xyz_17215_andso_on_01-file_08.html"
and want to split this at every character that is not a letter or number.
With
/[a-z0-9]/
I get an array with all the characters but what's the opposite of it to get all the words and numbers?

You can write:
$result_array = preg_split('/[^a-z0-9]+/', $string_to_split);

Rather than writing new code to solve a problem, use the built-in functionality that PHP provides to you in the parse_url() function: http://php.net/manual/en/function.parse-url.php

Related

What is the regular expression for all non-alphabetic characters in php?

I'm trying to parse a file and analyze it. To do this, I've used preg_split() to break the document into an array. I only want words in the array (otherwise alpha characters). The regular expression I used is:
$noAlpha = "/[\s]+|[^A-z]+|\W|\r/";
However, I'm getting instances of blanks in the array. I believe it has to do with a line with a return only (\r) and nothing else on it.
I'm only using .txt files. What would I need to add to the regex to account for this?
To extract all the words (only letters), you can use this
preg_match_all('/[^\W\d_]+/',$string,$matches)
If you want digits as well, then the pattern should be '/[^\W_]+/'
Try this:
$noAlpha = "/\s+|[^a-zA-Z]+|\W|\r/";
You can try this:
$noAlpha = "/\s*\W\s*/";
However, I also would extract the words with preg_match_all instead.

basic string separation in php

I have an alphanumeric string like 1234and5678.
I want to store the numbers preceding and i.e 1234 into one variable and the number after and i.e 5678 into another variable as shown below:
$var1=1234;
$var2=5678;
also what should do if i replace and by some random special characters like #$% etc.
Can you please help me out?
Thanks in advance.
$string = "1234and5678";
list($before, $after) = explode("and", $string);
This splits the string into two variables based on the delimeter ("and"), whatever is before and is saved in a variable called $before, whatever is after is saved into a variable called $after
Use split http://php.net/manual/en/function.split.php with "and" as separator. That should give you an array with the two numbers.
you can use preg_split() function, in your case: $var = preg_split("/[\D]+/", "1234and5678"); That gives you $var[0] = '1234' and $var[1] = '5678'
According to you edit, you can:
replace regex on any another that you need (e.g. $var = preg_split("/[\d]+/", "1234and5678"); for any non-numeric element)
use preg_match_all("/(\d+)/","1234and5678", $var) to find any numbers in your string
Use regular expressions. I provided a link for regexes in php. Split is deprecated as of version 5.3.0 and you should not rely on it.
Perl compatible regular expressions

How to match a regular expression that contains an arbitrary string, and get only that arbitrary string into a variable using PHP?

I'm trying to write a very simple markup language in PHP that contains tags like [x=123], and I need to be able to match that tag and extract only the value of x.
I'm assuming the answer involves regex but maybe I'm wrong.
So if we had a string:
$str = "F9F[x=]]^$^$[x=123]#3j3E]]#J";
And a regular expression to match:
/^\[x=.+\]$/
How would we get only the ".+" portion of the matching string into a variable?
You can use preg_match to search a string for a regular expression.
Check out the documentation here: http://www.php.net/manual/en/function.preg-match.php for more information on how to use it (as well as some examples). You might also want to take a look at preg_grep.
Following code should work for you:
$str = "F9F[x=]]^$^$[x=123]#3j3E]]#J";
if (preg_match('~\[x=(?<valX>\d+)\]~', $str, $match))
echo $match['valX'] . "\n";
OUTPUT:
123

php what is the equivalent of preg_match but does not require regex?

In PHP is there an equivalent to preg_match that does not require the use of regex? There is str_replace() for preg_replace. Is there something for preg_match.
*update * I am only looking to replace a known string with another. Using regex just seems like overkill.
I have the string "This is a [test1], and not a [test2]" and I want to match them with "[test1]" and "[test2]".
If you mean find a string within another string without using regex, you can use strpos
if (strpos('hello today', 'hello') !== false) {
// string found
}
Since I am not sure what result you are looking for I can't say if this is exactly what you are looking for.
You can use strpos to see if an occurrence of one string is in another.
To answer your question there is some function of PHP without regex
Do not use preg_match() if you only
want to check if one string is
contained in another string. Use
strpos() or strstr() instead as they
will be faster.
But they can not replace preg_match completely at all
First, str_replace() is not replacement for preg_replace(). Function str_replace() replaces all occurrences of the search string with the replacement string, preg_replace() replaces content selected by regular expressions (that's not same thing).
A lot of things require regex (and that's good) so you can't simply replace it with single PHP function.
Most developers use preg_match because they want to use the matches (the third parameter which will get set by the function).
I can not think of a function that will return or set the same information, as done with matches.
If however, you are using preg_match without regex then you might not care as much about the matches.
If you are using preg_match to see if there is a "match" and just that then I'd suggest using strpos instead, since it is much more efficient at seeing if one string is found in another.

help me understand PHP's preg_replace

I have this link:
http://mysite/myfolder/it/my-keywords.html and want to replace /it/ with /es/ (2 letter country codes)
I could explode() with "/" delimiter but would like to understand if preg_replace would be better.
tried:
preg_replace("/\/([a-z]{2})/\/", $link, $country);
EDIT
answer:
preg_replace("/\/[a-z]{2}\//", "/$country/", $link);
preg_replace is like a swiss army knife. preg rather than ereg means it uses perl compatible regular expressions. It matches first param (regex), replaces with second param (string) in third param (string).
Regular expressions are optimized for efficiency by using search tree cutoff techniques etc... so are generally efficient alternative method.
This should do what you want.
preg_replace("/\/it\//","/es/","http://mysite/myfolder/it/my-keywords.html")
preg_replace is useful if you only know a part of string that you want to match, but others parts are variable. In your case the /it/ folder is already unique enough so that a static replacement would work. Use str_replace instead:
$url = str_replace("/it/", "/es/", $url);

Categories