Replace multiple instances of PHP - 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-

Related

Manipulating a version number w/o regex?

I have a 4-digit version-number ("1234") and would like to insert "_" to separate the digits ("1_2_3_4").
The only idea I came up with was using regex:
<?
$str="1234";
$s1 = preg_replace("/\d/","$0_",$str,3);
echo "$str|$s1";
?>
But I have a nagging feeling there must be a more elegant way to achive this w/o rx, by using just the string-manipulation methods. Any suggestions?
Using your example string, you could also use str_split and implode in this case:
$str = '1234';
$newstr = implode('_', str_split($str));
echo $newstr; // 1_2_3_4

Remove everything between two strings

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

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"

How can I remove slashes from strings?

I am trying to do some PHP programming concepts and I am not aware of some in-build functions. So my doubt is:
In PHP, how to remove slashes from strings? Is there any function available in PHP for this??
e.g.
$string="people are using Iphone/'s instead of Android phone/'s";
You can do a number of things here, but the two approaches I would choose from are:
Use str_replace():
$string = "people are using Iphone/'s instead of Android phone/'s";
$result = str_replace('/','',$string);
echo $result;
// Output: people are using Iphone's instead of Android phone's
If the slashes are backward slashes (as they probably are), you can use stripslashes():
$string = "people are using Iphone\\'s instead of Android phone\\'s";
$result = stripslashes($string);
echo $result;
// Output: people are using Iphone's instead of Android phone's
backslashes need escaping
$newstr = "<h1>Hello \ fred</h1>";
echo str_replace('\\','',$newstr);
If it is a quoted string. Use stripslashes
Heres what I use
function removeSlashes($string = '')
{
return stripslashes(str_replace('/', '', $string));
}
Test
echo $this->removeSlashes('asdasd/asd/asd//as/d/asdzfdzdzd\\hd\h\d\h\dw');
Output
asdasdasdasdasdasdzfdzdzdhdhdhdw
you can use function like
$string = preg_replace ("~/~", "", $string);
Use varian preg
$string="people are using Iphone/'s instead of Android phone/'s";
echo $string = preg_replace('/\//', '', $string);
body, html, iframe {
width: 100% ;
height: 100% ;
overflow: hidden ;
}
<iframe src="https://ideone.com/uIBINP" ></iframe>
I tried this method to remove single forward slashes.
I used str_replace to strip the slashes out. It still did not work for me, I had to go and change all the double quotes in the database to single quotes, update the table, then change it back to double quotes for it to work. Weird.
str_replace('\\', '', $content)
You can use stripslashes() function.
<?php
$str = "Is your name O\'reilly?";
// Outputs: Is your name O'reilly?
echo stripslashes($str);
?>

regex for breadcrumb in php

I am currently building breadcrumb. It works for example for
http://localhost/researchportal/proposal/
<?php
$url_comp = explode('/',substr($url,1,-1));
$end = count($url_comp);
print_r($url_comp);
foreach($url_comp as $breadcrumb) {
$landing="http://localhost/";
$surl .= $breadcrumb.'/';
if(--$end)
echo '
<a href='.$landing.''.$surl.'>'.$breadcrumb.'</a>ยป';
else
echo '
'.$breadcrumb.'';
};?>
But when I typed in http://localhost////researchportal////proposal//////////
All the formatting was gone as it confuses my code.
I need to have the site path in an array like ([1]->researchportal, [2]->proposal)
regardless of how many slashes I put.
So can $url_comp = explode('/',substr($url,1,-1)); be turned into a regular expression to get my desired output?
You don't need regex. Look at htmlentities() and stripslashes() in the PHP manual. A regex will return a boolean value of whatever it says, and won't really help you achieve what you are trying to do. All the regex can let you do is say if the string matches the regex do something. If you put in a regex requiring at least 2 characters between each slash, then any time anyone puts more than one consecutive slash in there, the if statement will stop.
http://ca3.php.net/manual/en/function.stripslashes.php
http://ca3.php.net/manual/en/function.htmlentities.php
Found this on the php manual.
It uses simple str_replace statements, modifying this should achieve exactly what your post was asking.
<?
function stripslashes2($string) {
$string = str_replace("\\\"", "\"", $string);
$string = str_replace("\\'", "'", $string);
$string = str_replace("\\\\", "\\", $string);
return $string;
}
?>

Categories