How do I remove parts just from a word in a string and leave intact the rest of it
Having the following situation
Notebooks_Lenovo and Tablets
I would like to remove the part _Lenovo and get back as result
Notebooks and Tablets
I know as option I have Regex but is there a php function which would do the job
Try This
str_replace("_Lenovo", "", "Notebooks_Lenovo and Tablets");
Try str_replace() like,
str_replace("_Lenovo", "", "Notebooks_Lenovo and Tablets");
the str_replace() function can help you
str_replace("_Lenovo", "", "Notebooks_Lenovo and Tablets");
$string = str_replace("_Lenovo ", "", "Notebooks_Lenovo and Tablets");
echo $string;
Related
How i can add a slash or ; after every string. Here i have an Example:
This is what i have
"SKU" "TITLE" "LINK" "LINK2" "PRICE" "World of Warcraft"
But i will have like that
"SKU";"TITLE";"LINK";"LINK2";"PRICE";"World of Warcraft"
How i can made this? I work with affiliate and this Partner dont give CSV. Its only an TXT file.
So, how i can add slash into every "string"?
i already tried to add ; after every 2nd ". But it dont work with my code...
also tried with str_replace()
str_replace(" ", ";", $item);
The solution using preg_replace function:
$str = '"61926182767182" "DAS GEILE GAME" "HTTP://google.com/123129182691239" "HTTP://google.com/123129182691239" "32.59"';
$str = preg_replace("/(\"[^\"]+\")\s/", "$1;", $str);
print_r($str); // '"61926182767182";"DAS GEILE GAME";"HTTP://google.com/123129182691239";"HTTP://google.com/123129182691239";"32.59"'
(this approach can be easily extended if there would be tabs instead of spaces in the input string OR if there may be multiple spaces between words. It's more flexible)
You can split string by " " delimiter using explode() and join result array with ";" string using implode()
$str = implode('";"', explode('" "', $str))
See result in demo
Need to remove everything between .jpg and > on all instances like these below:
.jpg|500|756|20121231-just-some-image-3.jpg)%>
.jpg|500|729|)%>
.jpg|500|700|)%>
.jpg|500|756|test-43243.jpg)%>
So everything becomes .jpg>
Any suggestions using preg_replace?
preg_replace('/\.jpg[^>]+>/', '.jpg>', $your_string);
$str = '.jpg|500|756|20121231-just-some-image-3.jpg)%>';
preg_replace('/[^\.jpg][^>]+/', '', $str);
When I've a string:
$string = 'word1="abc.3" word2="xyz.3"';
How can I replace the point with a comma after xyz in xyz.3 and keep him after abc in abc.3?
You've provided an example but not a description of when the content should be modified and when it should be kept the same. The solution might be simply:
str_replace("xyz.", "xyz", $input);
But if you explicitly want a more explicit match, say requiring a digit after the ful stop, then:
preg_replace("/xyz\.([0-9])+/", 'xyz\${1}', $input);
(not tested)
something like (sorry i did this with javascript and didn't see the PHP tag).
var stringWithPoint = 'word1="abc.3" word2="xyz.3"';
var nopoint = stringWithPoint.replace('xyz.3', 'xyz3');
in php
$str = 'word1="abc.3" word2="xyz.3"';
echo str_replace('xyz.3', 'xyz3', $str);
You can use PHP's string functions to remove the point (.).
str_replace(".", "", $word2);
It depends what are the criteria for replace or not.
You could split string into parts (use explode or preg_split), then replace dot in some parts (eg. str_replace), next join them together (implode).
how about:
$string = 'word1="abc.3" word2="xyz.3"';
echo preg_replace('/\.([^.]+)$/', ',$1', $string);
output:
word1="abc.3" word2="xyz,3"
I would like to replace ":" with "\xEF\xBC\x9A" however, I do not want to do this for : that follows "http", "https", and string with the format of #[{numbers}:{numbers}:{some text}].
What I have doesn't really work as (?<!#\[\d) does not check what's after it. My current implementation only work for something like #[1:2:text]. Thanks!
$string=preg_replace('#(?<!https)(?<!http)(?<!#\[\d)(?<!#\[\d:\d):#', "\xEF\xBC\x9A", $string);
Try this:
preg_replace('/(#\[\d+:\d+:[^]]*]|https?:)|:/e', '"$1"?"$1":"\xEF\xBC\x9A"', $string);
Try this regex:
(?<!#\[(?::?[\d]{0,5}){1,2})(?<!https?):
It should match the first and second instances of ':' here.
: test: http: #[1:2:text]
Usage Sample:
$string = preg_replace('/(?<!#\[(?::?[\d]{0,5}){1,2})(?<!https?):/', '\xEF\xBC\x9A', $string);
i'm just wondering how I can replace multiple instances of - with just one using php,
for example say I have
test----test---3
what could I do to replace the multiple instances of - with just 1 so it would be
test-test-3
thanks :)
Remove every repeating character:
$string = 'test----test---3';
echo preg_replace('{(.)\1+}','$1',$string);
Remove specific repeating character:
$string = 'test----test---3';
echo eregi_replace("-{2,}", "-", $string);
Remove specific repeating character the 'ugly' way:
$string = 'test----test---3';
echo implode('-',array_filter(explode('-',$string)));
Result for all snippets:
test-test-3
Uhm...
function replaceDashes($str){
while(strpos($str,'--')!==false)
$str=str_replace('--','-',$str);
return $str;
}
You can make it "faster" be replacing:
$str=str_replace('--','-',$str);
With:
$str=str_replace(array('----','---','--'),'-',$str);
As eregi_replace and ereg_replace is depreciated in PHP5, you can also try
preg_replace("/-{2,}/", "-", $string);
So if you run
preg_replace("/-{2,}/", "-", "--a--b---c----")
it will return
-a-b-c-