How to remove comma from string [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have string like this
$value = "abc , cde";
I want to remove that " , " from the sring and display like this
$value = "abc cde";
how to do this?

A simple str_replace should work here:
If you want to remove the comma and the spaces around it and replace them with a single space:
$value = str_replace(" , ", " ", $value);
If you want to just remove the comma only and leave the spaces around it intact:
$value = str_replace(",", "", $value);

Try with str_replace like
echo str_replace("," , " " , $value);
See this Manual

str_replace
Here everything is in detail: http://php.net/manual/en/function.str-replace.php

Try using str_replace : http://www.php.net/manual/en/function.str-replace.php
$value = str_replace(',', '', $value);

Just use simple function str_replace
echo str_replace(",", "", $value);

Related

PHP: removing words written in an array from another word [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
$a = array('jo', 'se');
$word= 'josephine';
I want to delete all the instances where the words 'jo' or 'se' can be found in the $word. So it would print the new word 'phine'.
str_replace can do this natively:
<?php
$a = array('jo', 'se');
$word = 'josephine';
$word = str_replace($a, '', $word);
var_dump($word); //string(5) "phine"
?>
DEMO
str_replace()
$a = array('jo', 'se');
$word= 'josephine';
echo str_replace($a, '', $word);
or using strtr()
echo strtr($word, $a);
Refer to str_replace in the manual
$a = array('jo', 'se');
$word= 'josephine';
$string = str_replace($a, "", $word);
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

Convert comma separated string into list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string eg:
$string = "word1,word2,word3,word4";
I need to echo this into <li> elements using PHP. So $string becomes:
<li>word1</li>
<li>word2</li>
<li>word3</li>
<li>word4</li>
Like this:
$string = "word1,word2,word3,word4";
$string = explode(",",$string);
foreach ($string as $str) {
echo "<li>".$str."</li>";
}
You can explode() the string into an array, loop through it, and output the results into a list option.
Try this:
echo "<li>" . str_replace ("," , "</li><li>" , $string) . "</li>";
For what you are trying to accomplish, the explode approach adds unnecessary overhead.
You may try this
$string = "word1,word2,word3,word4";
echo "<ul>";
foreach(explode(',', $string) as $li) {
echo "<li>$li</li>";
}
echo "</ul>";
DEMO.

Output Value conversion in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am getting Output value like 0.000098, 0.000854.
But I need 98, 854 instead.
How to do this?
<?php
$str = '0.00009801';
$r = array();
$ret = preg_match('/[1-9]+[0-9]+/', $str, $r);
echo $r[0];
You can use
$num = '0.000098000';
$newNum = preg_replace('/^[0\.]+/', '', $num);
The $newNum will contain the required output (98000 in the case above) after replacing all the 0 and . from the starting.
So you want to take a string, split it by a comma and remove spaces , multiply by 1000000 then put it back together. Try doing this:
$str = "0.000098, 0.000854";
echo implode(",",array_map(function($a){ return 1000000 * trim($a); },explode(",", $str)));
or in a slightly more readable form:
$parts = explode(",", $str);
$parts = array_map(function($a) { return 1000000 * trim($a); }, $parts);
echo implode(",", $parts);
function convertVal($value) {
for($i=0;$i<strlen($value);$i++){
$a = strpos($value,substr($value,$i,$i+1));
if($a>0){
$pos = substr($value,$a-1,strlen($value));
}
}
return $pos;
}
echo convertVal("0000980");

Replace first word of sentence to the end if it is "the" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If the first word of a sentence is "The" I want to replace it to the end with a comma before it so for example if I have the movie title: The Hunger Games, I want it to become: Hunger Games, The
Is this possible and if it is how can I get it working?
Please try this:
<?
$str = 'The Hunger Games';
if (strtolower(substr($str,0,3)) == "the"){
$str = trim(substr($str,3)).', The';
}
echo $str;
?>
WORKING CODE
Use:
echo custom_func('The Hunger Games');
Function:
function custom_func($s) {
$s = trim(preg_replace('~\s+~', ' ', $s));
$a = explode(' ', $s);
if (strtolower($a[0]) != 'the' || count($a) < 2) return $s;
$the = array_shift($a);
return implode(' ', $a) . ', ' . $the;
}
Demo.

Replace keyword in a heading with php or jquery? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to replace keywords such as "the", "and" etc.. in headings and replace it with a span in the heading tags.
Ex:
<h2>This is the heading</h2>
to become
<h2>This is <span>the</span> heading</h2>
Thanks for any help
UPDATE
I found something that works for what I had wanted:
$(function() {
$('h2').each(function(i, elem) {
$(elem).html(function(i, html) {
return html.replace(/the/, "<span>the</span>");
});
});
});
A PHP only solution (without regex):
$string = "<h2>This is the heading</h2>";
$toReplace = array("the", "and");
$replaceTo = array_map(function ($val) { return "<span>$val</span>"; }, $toReplace);
$newString = str_replace($toReplace, $replaceTo, $string);
print $newString; // prints as expected: <h2>This is <span>the</span> heading</h2>
This code will help you do it and extend your words dynamically:
$special_words = array("the", "and", "or") ;
$words = implode("|", $special_words) ;
$string = "<h2>This is the heading</h2>" ;
$new = preg_replace("/({$words})/i", "<span>$1</span>", $string) ;
echo $new ;
very easy with regexp, this example only use one keyword, for multiple keywords use an array.
$string = "<h2>This is the heading</h2>";
$string = preg_replace("/(the|end)/", "<span>$1</span>", $string);

Categories