What I'm trying to do is very simple, but I'm looking to do it most efficiently, preferably using php builtin fns.
$str = '1234';
echo replace_function(array('1','3'),array('3','1'),$str);
// output: 3214
str_replace,preg_replace would result in 1214, which means it goes through the arrays, replacing matched strings. I'm looking for a solution to simultaneously "switch" these two (or more) strings.
any ideas?
You need string translate:
http://php.net/manual/en/function.strtr.php
<?php
$trans = array("hello" => "hi", "hi" => "hello");
echo strtr("hi all, I said hello", $trans);
// = hello all, I said hi
?>
<?php
$subject = '1234';
$result = preg_replace('/(1)(2)(3)(4)/si', '$3$2$1$4', $subject);
var_dump($result);
?>
You can change the pattern to something more generic, such as '/(\d)(\d)(\d)(\d)/'.
Related
I want to be able to specify an index in a string and remove it.
I have the following:
"Hello World!!"
I want to remove the 4th index (o in Hello). Here would be the end result:
"Hell World!!"
I've tried unset(), but that hasn't worked. I've Googled how to do this and that's what everyone says, but it hasn't worked for me. Maybe I wasn't using it right, idk.
This is a generic way to solve it:
$str = "Hello world";
$i = 4;
echo substr_replace($str, '', $i, 1);
Basically, replace the part of the string before from the index onwards with the part of the string that's adjacent.
See also: substr_replace()
Or, simply:
substr($str, 0, $i) . substr($str, $i + 1)
$str="Hello World";
$str1 = substr($str,0,4);
$str2 = substr($str,5,7);
echo $str1.$str2;
This php specific of working with strings also bugged me for a while. Of course natural solution is to use string functions or use arrays but this is slower than directly working with string index in my opinion. With the following snippet issue is that in memory string is only replaced with empty � and if you have comparison or something else this is not good option. Maybe in future version we will get built in function to remove string indexes directly who knows.
$string = 'abcdefg';
$string[3] = '';
var_dump($string);
echo $string;
$myVar = "Hello World!!";
$myArray = str_split($myVar);
array_splice($myArray, 4, 1);
$myVar = implode("", $myArray);
Personal I like dealing with arrays.
(Sorry about lack of code brackets putting this up via my phone)
I think can create a function and call it like this
function rem_inx ($str, $ind)
{
return substr($str,0,$ind++). substr($str,$ind);
}
//use
echo rem_inx ("Hello World!!", 4);
I have a string with a different variety of strings of different lengths
Example:
/my-big-property/Residential/Sections-for-sale
/my-big-property/Residential/for-sale
I am wanting to only remove /my-big-property/ but because substr does not seem to work what other options do I have?
Can you explain further by substr doesn't work? This seems like an awfully simple problem.
<?php
$a = "/my-big-property/Residential/Sections-for-sale";
$b = substr($a, 17);
echo $b;
If the initial string between the first / and second / is variable, then regex such as this would suffice:
<?php
$a = "/my-big-property/Residential/Sections-for-sale";
preg_match("/\/\S+?\/(.*)/", $a, $matches);
print_r($matches);
This would output:
Array
(
[0] => /my-big-property/Residential/Sections-for-sale
[1] => Residential/Sections-for-sale
)
substr is working fine, you are just not using it properly. It is a function not a procedure.
It does not change the original string but returns a new substring.
Above all the suggested solutions which are correct you can simply use the following:
$string="/my-big-property/Residential/Sections-for-sale";
$string = str_replace("/my-big-property/", "", $string);
<?php
$a = "/my-big-property/Residential/Sections-for-sale";
$temp = explode('/my-big-property/',$a);
$temp_ans = $temp[1];
echo $temp_ans;
?>
There will be two array one will be blank and another will have desired value.
I'm searching a function to cut the following string and get all content BEFORE and AFTER
I need this part<!-- more -->and also this part
Result should be
$result[0] = "I need this part"
$result[1] = "and also this part"
Appreciate any help!
Use the explode() function in PHP like this:
$string = "I need this part<!-- more -->and the other part.
$result = explode('<!-- more -->`, $string) // 1st = needle -> 2nd = string
Then you call your result:
echo $result[0]; // Echoes: I need that part
echo $result[1]; // Echoes: and the other part.
You can do this pretty easily with regular expressions. Somebody out there is probably crying for parsing HTML/XML with regular expressions, but without much context, I'm going to give you the best that I've got:
$data = 'I need this part<!-- more -->and also this part';
$result = array();
preg_match('/^(.+?)<!--.+?-->(.+)$/', $data, $result);
echo $result[1]; // I need this part
echo $result[2]; // and also this part
If you are parsing HTML, considering reading about parsing HTML in PHP.
Use preg_split. Maybe something like this:
<?php
$result = preg_split("/<!--.+?-->/", "I need this part<!-- more -->and also this part");
print_r($result);
?>
Outputs:
Array
(
[0] => I need this part
[1] => and also this part
)
I need to replace multiple text with it's associated text
Example: I have string like: "apple is a great fruit"
Now I need to replace "apple" to "stackoverflow", and "fruit" to "website"
I know I can use str_replace, but is there any other way? str_replace would be slow in my case, because I need to replace atleast 5 to 6 words
Help appreciated.
<?php
$a = array('cheese','milk');
$b = array('old cow','udder');
$str = 'I like cheese and milk';
echo str_replace($a,$b,$str); // echos "I like old cow and udder"
Alternatively, if you don't like to look of that (worried of miss matching array values) then you could do:
$start = $replace = $array();
$str = "I like old cow and udder"
$start[] = 'cheese';
$replace[] = 'old cow';
$start[] = 'milk';
$replace[] = 'udder';
echo str_replace($start,$replace,$str); // echos "I like old cow and udder"
edit: I see dnl has edited the question and put emphasis on the fact that str_replace would be too slow. In my interpretation of the question it is because the user was not aware that they would use arrays in str_replace.
if u know all the word sequence and also replaced word then use below technique
$trans = array( "apple" => "stackoverflow", "fruit" => "website");
echo strtr("apple is a great fruit", $trans); // stackoverflow is a great website
Reference
For your purpose str_replace seams to be already the fastest solution.
str_replace is faster than strstr
Source: http://www.simplemachines.org/community/index.php?topic=175031.0
I have a string, for example "[{XYZ123}] This is a test" and need to parse out the content in between the [{ and }] and dump into another string. I assume a regular expression is in order to accomplish this but as it's not for the faint of heart, I did not attempt and need your assistance.
What is the best way to pull the fragment in between the [{ and }]? Thanks in advance for your help!
<?php
$str = "[{XYZ123}] This is a test";
if(preg_match('/\[{(.*?)}\]/',$str,$matches)) {
$dump = $matches[1];
print "$dump"; // prints XYZ123
}
?>
$str = "[{XYZ123}] This is a test";
$s = explode("}]",$str);
foreach ($s as $k){
if ( strpos($k,"[{") !==FALSE ){
$t = explode("[{",$k); #or use a combi of strpos and substr()
print $t[1];
}
}
The regex would be (?=\[\{).*(?=\}\]), though I don't know if php supports look aheads.