Consider this:
$sServerPath = "\\\\nlyehvedw1cl016\\projects$\\ARCLE_SW_SVN\\";
$sSVNParentPath = $sServerPath."svn\\";
$bla = "
authz_module_name = TEST_TestRepos
repository_dir = bla
W";
$sSVNParentPath = $sServerPath."svn\\";
$sReplaceBy = "repository_dir = ".$sSVNParentPath.$sProjectName."\n";
echo $sReplaceBy;
echo preg_replace ('/repository_dir = ([a-zA-Z0-9\/].*?)\n/i', $sReplaceBy, $bla);
The result is:
repository_dir = \\nlyehvedw1cl016\projects$\ARCLE_SW_SVN\svn\
authz_module_name = TEST_TestRepos
repository_dir = \nlyehvedw1cl016\projects$\ARCLE_SW_SVN\svn\
W
The echo of $sReplaceBy shows the resulting string as I expect it, including the first 2 back-slashes.
However, after the preg_replace, the echo of the result shows only one back-slash!
Anybody know what's causing this?
From PHP docs:
To use backslash in replacement, it must be doubled ("\\" PHP string).
Since your replacement doesn't contain quotes, you can simply use addslashes():
echo preg_replace ('/repository_dir = ([a-zA-Z0-9\/].*?)\n/i', addslashes($sReplaceBy), $bla);
Related
I know the title may be a little vague but I wasnt sure how to formulate it. I have a string that contains text that looks something like this:
$data["key1"] = "value1";
$data["key2"] = "value2";
$data["key3"] = "value3";
$data["key4"] = "value4";
I would like to match everything after $data[" and ]" and everything in between = " and "; in the same match, so for example the results would be
Match 1 = {key1, value1}
Match 2 = {key2, value2}
Match 3 = {key3, value3}
Match 4 = {key4, value4}
So far I have been able to match the values with
/(?<=]\s=\s\")(.*?)(?=\s*\"\;)/
but I would also need the first part in the result as well and I'm not sure how to do so.
Also, is there a way to have it match if there is (or isn't) white spaces between characters?
for example
$data["key1"]= "value1";
$data["key2"]="value2";
$data["key3"] ="value3";
$data["key4"] ="value4" ;
Would also all match the same thing?
Try using preg_match_all:
$input = '$data["key1"] = "value1";';
preg_match_all('/\$\w+\["(.*?)"\]\s*=\s*"(.*?)";/', $input, $matches);
echo "Match = {" . $matches[1][0] . ", " . $matches[2][0] . "}";
This prints:
Match = {key1, value1}
You can try the following for each line. Basically you just need to search for each pair of quotes in each line should do the work.
// $output_array is an array which the first index is your key and second is the value
// for example, array( "key1", "value1")
$input_lines = '$data["key1"] = "value1"';
preg_match_all('/\"\w+\"/', $input_lines, $output_array);
I have a variable that I echo, like this:
echo "hm={$yes["n"]}";
I need to replace every instance of (whitespace) with +. What I tried was putting this:
str_replace(" ","+",{$yes["n"]});
before I echoed it out.
It said unexepected {, so I tried:
str_replace(" ","+",$yes["n"]);
Where nothing happened.
You have forgotten to assign the output of str_replace to the variable.
$yes["n"] = 'string with whitespace';
$yes["n"] = str_replace(" ","+",$yes["n"]);
echo "hm={$yes["n"]}"; // echoes hm=string+with+whitespace
I am not the best with RegEx... I have some PHP code:
$pattern = '/activeAds = \[(.*?)\]/si';
$modData = preg_replace($pattern,'TEST',$data);
So I have a JavaScript file, and it declares and array:
var activeAds = [];
I need this to populate the array with my string, or if the array already has a string inside it, i want to replace it with my string (in this case "TEST").
Right now, my REGEX is replacing everything, including my start and end, i need to only replace whats between.
I'm left with:
var TEST;
TIA
You could capture what's before and what's after the part you want replacing:
$pattern = '/(activeAds = \[).*?(\])/si';
After capturing these parts, you can keep them and replace the part in the middle:
$modData = preg_replace($pattern, '\1TEST\2', $data);
There are many ways you could do this, mine is below:
$data = array("activeAds = testing123");
$pattern = "/activeAds\s?=\s?(.*)/";
$result = preg_replace($pattern,"activeAds = TEST", $data);
var_dump($result);
Edit: Forgot to mention that the \s? here allow for an optional space.
Could you please help me to a string inside a string when start and end are given. Actually I want to delete all the contents between //[langStart-en] and //[langEnd-en] in the following example
//[langStart-en] This is a test //[langEnd-en]
using preg_replace. I used the following code
$string = '//[langStart-en] This is a test //[langEnd-en]';
$pattern = '/\/\/\[langStart-en\][^n]*\/\/\[langEnd-en\]/';
$replacement = '//[langStart-en]//[langEnd-en]';
$my_string = preg_replace($pattern, $replacement, $string);
echo $my_string;
It is showing the following error
Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' : eval()'d code on line 4"
Please help
Why remove the string between the given strings if you can concatenate the strings you are given. They will give you the same result.
string c = a + b;
Here you go:
//[langStart-en]//[langEnd-en]
For those of you with less sense of humour - shame on you. But here's an answer.
var str = '//[langStart-en] This is a test //[langEnd-en]';
str.replace(/\/\/\[langStart-en\].+\/\/\[langEnd-en\]/g, '//[langStart-en]//[langEnd-en]');
Yah, I don't mean to be sarcastic.
$my_string = '//[langStart-en]//[langEnd-en]';
Or
$string = '//[langStart-en] This is a test //[langEnd-en]';
$pattern = '/\/\/\[langStart-en\][^n]*\/\/\[langEnd-en\]/';
$replacement = '//[langStart-en]//[langEnd-en]';
$my_string = preg_replace($pattern, $replacement, $string);
echo $my_string;
Not sure what language your using to do this.
But most languages have an indexof function.
var mystring - "cccctestoooabcccc";
var i = mystring.indexof("test");
var x = mystring.indexof("abc");
With those indexs you can use a function like substring(startindex, endindex);
Although, you will have to add or subtract the length of your string (test or abc)
Because the the index is of the first character location.
So
i = 4 and x = 11
you'd would want to pull the substring between ((i + "test".length), x)
Hopefully pull the substring "ooo"
This is rough, but should give you the general idea.
$string = '//[langStart-en] This is a test //[langEnd-en]';
$string = preg_replace(
'/\/\/\[langStart-en\][\s\S]+?\/\/\[langEnd-en\]/',
'//[langStart-en]//[langEnd-en]',
$string
);
I have a string, "Chicago-Illinos1" and I want to add one to the end of it, so it would be "Chicago-Illinos2".
Note: it could also be Chicago-Illinos10 and I want it to go to Chicago-Illinos11 so I can't do substr.
Any suggested solutions?
Complex solutions for a really simple problem...
$str = 'Chicago-Illinos1';
echo $str++; //Chicago-Illinos2
If the string ends with a number, it will increment the number (eg: 'abc123'++ = 'abc124').
If the string ends with a letter, the letter will be incremeted (eg: '123abc'++ = '123abd')
Try this
preg_match("/(.*?)(\d+)$/","Chicago-Illinos1",$matches);
$newstring = $matches[1].($matches[2]+1);
(can't try it now but it should work)
$string = 'Chicago-Illinois1';
preg_match('/^([^\d]+)([\d]*?)$/', $string, $match);
$string = $match[1];
$number = $match[2] + 1;
$string .= $number;
Tested, works.
explode could do the job aswell
<?php
$str="Chicago-Illinos1"; //our original string
$temp=explode("Chicago-Illinos",$str); //making an array of it
$str="Chicago-Illinos".($temp[1]+1); //the text and the number+1
?>
I would use a regular expression to get the number at the end of a string (for Java it would be [0-9]+$), increase it (int number = Integer.parse(yourNumberAsString) + 1), and concatenate with Chicago-Illinos (the rest not matched by the regular expression used for finding the number).
You can use preg_match to accomplish this:
$name = 'Chicago-Illinos10';
preg_match('/(.*?)(\d+)$/', $name, $match);
$base = $match[1];
$num = $match[2]+1;
print $base.$num;
The following will output:
Chicago-Illinos11
However, if it's possible, I'd suggest placing another delimiting character between the text and number. For example, if you placed a pipe, you could simply do an explode and grab the second part of the array. It would be much simpler.
$name = 'Chicago-Illinos|1';
$parts = explode('|', $name);
print $parts[0].($parts[1]+1);
If string length is a concern (thus the misspelling of Illinois), you could switch to the state abbreviations. (i.e. Chicago-IL|1)
$str = 'Chicago-Illinos1';
echo ++$str;
http://php.net/manual/en/language.operators.increment.php