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.
Related
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 UTF8 string that contains letters and digits. For example:
"Hello World 37. What? 24 last 6650"
and I want to reverse only the digits but keep the numbers in the same place.
The output should be:
"Hello World 73. What? 42 last 0566"
echo preg_replace_callback('/\d+/', function (array $m) { return strrev($m[0]); }, $string);
Before I posted the question, I thought about it and got an idea that works for me, so I'm only posting this question to enrich the database.
function reverseNumbersInString($str){
$tokens = explode(" ", $str);
$res = "";
for ($i = 0; $i < sizeof($tokens); $i++){
if (intval($tokens[$i] > 0 )){
$tokens[$i] = strrev($tokens[$i]);
}
$res .= " " . $tokens[$i];
}
return $res;
}
Maybe something like this:
$string = "Hello World 37. What? 24 last 6650";
preg_match_all('/\d+/', $string, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $numberData) {
$numberArray = str_split($numberData[0]);
$reversedNumber = implode('', array_reverse($numberArray));
$string = substr_replace($string, $reversedNumber, $numberData[1], strlen($numberData[0]));
}
This should do it:
$stringWithReversedNumbers = preg_replace_callback(
'/\d+/',
function ($matches) {
return strrev($matches[0]);
},
$originalString
);
Alternatively, if you ONLY want to operate on numbers that are distinct words (i.e. not part of another word, as in if you want hello123goodbye to remain unmodified because the 123 isn't a word by itself), change \d+ to \b\d+\b
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");
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.
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);
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);