Remove everything between two strings - php

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);

Related

PHP replace smiley by img tag

I am trying to replace smileycodes by a img tag.
I want to replace this:
:blush:
by:
<img src='images/blush.png' />
"blush" is a variabele, so it can be any smiley.
I have to replace everything between the colons. I am not familiar with regex.
Can you help me?
For multiple replacements you could use str_replace with arrays instead of strings to search for, having an array for smiley-codes to be replaced by values of the second array. But you need to configure all values in arrays what is kind of static.
Another solution was to loop over possible smiley-codes and do a str_replace for each of it:
$possibleCodes = array('blush', 'smiley2', 'smiley3');
foreach($possibleCodes as $code) {
str_replace(':'.$code.':', "<img src='images/".$code.".png'", $yourString);
}
This requires to have the image name same as the smiley-code.
Why use regex if you aren't familiar with it? It is very simple and easy to do without regex.
str_replace(":blush:", "<img src='images/blush.png' />", $myString);
You can use preg_replace like as
preg_replace('~(:blush:)~','<img src="images/blush.png" />',$your_string);
Edited
If you had an array of string to replace then you can simply use str_replace like as
$find_array = array('blush', 'smiley2', 'smiley3');
$replace_array = ['<img src="images/blush.png" />','<img src="images/smiley2.png" />','<img src="images/smiley3.png" />']
str_replace($find_array,$replace_array,$your_string);

substring remove just from word with php

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;

Replace a character only in one special part of a string

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"

Preg_match, Replace and back to string

sorry but i cant solve my problem, you know , Im a noob.
I need to find something in string with preg_match.. then replace it with new word using preg_replace, that's ok, but I don't understand how to put replaced word back to that string.
This is what I got
$text ='zda i "zda"';
preg_match('/"(\w*)"/', $text);
$najit = '/zda/';
$nahradit = 'zda';
$o = '/zda/';
$a = 'if';
$ahoj = preg_replace($najit, $nahradit, $match[1]);
Please, can you help me once again?
You can use e.g. the following code utilizing negative lookarounds to accomplish what you want:
$newtext = preg_replace('/(?<!")zda|zda(?!")/', 'if', $text)
It will replace any occurence of zda which is not enclosed in quotes on both sides (i.e. in U"Vzda"W the zda will be replaced because it is not enclosed directly into quotes).

Replace multiple instances of PHP

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-

Categories