PHP Split Problem - php

I am trying to use (and I've tried both) preg_split() and split() and neither of these have worked for me. Here are the attempts and outputs.
preg_split("^", "ItemOne^ItemTwo^Item.Three^");
//output - null or false when attempting to implode() it.
preg_split("\^", "ItemOne^ItemTwo^Item.Three^");
//output - null or false when attempting to implode() it. Attempted to escape the needle.
//SAME THING WITH split().
Thanks for your help...
Christian Stewart

split is deprecated. You should use explode
$arr = explode('^', "ItemOne^ItemTwo^Item.Three^");

Try
explode("^", "ItemOne^ItemTwo^Item.Three^");
since your search pattern isn't a regex.

Are you sure you're not just looking for explode?
explode('^', 'ItemOne^ItemTwo^Item.Three^');

Since you are using preg_split you are trying to split the string by a given regular expresion. The circumflex (^) is a regular expression metacharacter and therefore not working in your example.
btw: preg_split is an alternative to split and not deprecated.

Related

php preg_split first uppercase

Using the following code,
php explode at capital letters?
you can explode the string by uppercase. But how do you explode it only on the first uppercase? Say you have helloThereMister. I want to get hello ThereMister. I can concatenate the result from the link above, but if there is a way to skip it, then fantastic!
Thanks
RTM my friend, as per documentation of preg_split you have also a $limit parameter so the answer is:
$pieces = preg_split('/(?=[A-Z])/', $str, 1);
look at manual for preg_split, third argument

preg_split url with a regular expression

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

How to use regex to append string to the matched results

I'm trying to replace 0-1. with 0-1.<br> how do i do that?
update:
Sorry for my vague question. You guys misunderstood me. '0-1.' is the pattern i want to replace, which means the pattern should be like `"/(\d)+(-)*(\d)*\./"` and the string may be '1.' '0-1.' or something that expression could represent
You can use a standard PHP function:
str_replace('0-1.', '0-1.<br>', $yourString);
How about:
preg_replace("/(\d+(?:-\d+)?\.)/", "$1<br>", $string);
You can use preg_replace like this:
preg_replace("/(0-1\.)/", "$1<br>", $string);
or, as you know the substitution already:
preg_replace("/0-1\./", "0-1.<br>", $string);

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.

PHP: Preg replace string with nothing

I'm new to preg_replace() and I've been trying to get this to work, I couldn't so StackOverflow is my last chance.
I have a string with a few of these:
('pm_IDHERE', 'NameHere');">
I want it to be replaced with nothing, so it would require 2 wildcards for NameHere and pm_IDHERE.
But I've tried it and failed myself, so could someone give me the right code please, and thanks :)
Update:
You are almost there, you just have to make the replacement an empty string and escape the parenthesis properly, otherwise they will be treated as capture group (which you don't need btw):
$str = preg_replace("#\('pm_.+?', '.*?'\);#si", "", $str);
You probably also don't need the modifiers s and i but that is up to you.
Old answer:
Probably str_replace() is sufficient:
$str = "Some string that contains pm_IDHERE and NameHere";
$str = str_replace(array('pm_IDHERE', 'NameHere'), '', $str);
If this is not what you mean and pm_IDHERE is actually something like pm_1564 then yes, you probably need regular expressions for that. But if NameHere has no actual pattern or structure, you cannot replace it with regular expression.
And you definitely have to explain better what kind of string you have and what kind of string you have want to replace.

Categories