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
Need to transform
PROP_RESOURCE_TYPE
to
propResourceType
or
setPropResourceType
Number of _ and string length are not fixed.
I'm not really into preg_replace,
could you help me out
lcfirst(implode(array_map('ucfirst', explode('_', strtolower($str)))));
For completeness, an example of how a regular expression could help:
// match each underscore followed by a letter
echo preg_replace_callback('/_([a-z])/', function($m) {
return strtoupper($m[1]); // upper case the letter
}, strtolower('PROP_RESOURCE_TYPE'));
One possible solution:
<?php
$step1 = strtolower("PROP_RESOURCE_TYPE"); // prop_resource_type
$step2 = str_replace("_"," ",$step1); // prop resource type
$step3 = ucwords($step2); // Prop Resource Type
$step4 = str_replace(" ","",$step3); // PropResourceType
$step5 = "set".$step4;
?>
For additional completeness, an example of how a regular expression could help:
$subject = 'PROP_RESOURCE_TYPE';
// match uppercase varnames
echo preg_replace_callback('/[A-Z]+(?:_[A-Z]+)+/', function ($matches) {
list($varName) = $matches;
$varName = strtolower($varName);
$varName = strtr($varName, '_', ' ');
$varName = ucwords($varName);
$varName = strtr($varName, [' ' => '']);
$varName = 'set' . $varName;
// or:
// $varName[0] = strtolower($varName[0]);
return $varName;
}, $subject);
// php 5.3.0+
$initial = 'PROP_RESOURCE_TYPE';
$words = explode('_', $initial);
foreach($words as &$word){
$word = ucfirst(strtolower($word));
}
$final = implode($words);
echo $final;
// returns PropResourceType
// if you want a lowercase first letter just do
echo lcfirst($final);
// returns propResourceType
another solution
echo preg_replace("/_([a-z])/e", 'strtoupper("$1")', strtolower($str));
EDIT e modifier has been deprecated as of PHP 5.5.0, so be carefull
EDIT2 #hakre was so angry, that I decided to mention, that the only problem is: e can cause E_DEPRECATED level error (usually, turned off on all production servers). Anyway it still works on all PHP versions
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
$string = "Hello World";
I want to place "_" randomly in $string so the output would be,
_H_e_ll_o W_orl_d
I've searched and I'm unable to find a working solution.
You can use mt_rand(0, 1) randomize! May be this code might work!
$string = "Hello World";
$string = str_split($string);
$final = "";
foreach ($string as $char)
$final .= ((mt_rand(0, 1) == 0) ? "_" : "") . $char;
echo $final;
After running a few times, I am getting this input:
H_ello W_or_l_d
He_llo W_o_rld
H_e_ll_o _W_o_r_ld
_Hell_o _W_orl_d_
He_l_lo _Wor_ld
_He_ll_o Wor_ld
Elaborating on Praveen Kumar's answer, but with more of a functional twist, we might do:
function underscorize($s, $t)
{
return $s . ((mt_rand(0, 1) == 0) ? "_" : "") . $t;
}
$string = "Hello World";
echo array_reduce(str_split("Hello World"), "underscorize");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
exp. string to replace;
150941_3D-glass-green-effect-hd-wallpapers_jpg
I need to replace the character _jpg with .jpg but the extension can change so I need _ to be replaced with a point. I have tried strpos and then substr_replace but this removes everything after and include the _.
Tested with 2 extensions and also accounts for jpeg
$str1 = '150941_3D-glass-green-effect-hd-wallpapers_jpg';
$str2 = '150941_3D-glass-green-effect-hd-wallpapers_png';
$str1 = preg_replace('/_([a-z]{3,4})$/', '.$1', $str1);
$str2 = preg_replace('/_([a-z]{3,4})$/', '.$1', $str2);
echo $str1; //150941_3D-glass-green-effect-hd-wallpapers.jpg
echo $str2; //150941_3D-glass-green-effect-hd-wallpapers.png
Use the str_replace command.
$string = '150941_3D-glass-green-effect-hd-wallpapers_jpg';
$new = '.jpg' // or wathever you want.
$new_string = str_replace('_jpg', $new, $string);
You could do this:
$filename = "150941_3D-glass-green-effect-hd-wallpapers_jpg";
$position = strrpos($filename, "_");
if($position !== false) {
$filename = substr_replace($filename, ".", $position, 1);
}
Check below code
$imgExt = array('_jpg','_jpeg','_png','_bmp','_gif');
$replaceExt = array('.jpg','.jpeg','.png','.bmp','.gif');
$string = '150941_3D-glass-green-effect-hd-wallpapers_jpg';
$replacedText = str_replace($imgExt,$replaceExt,$string);
<?php
$o = "150941_3D-glass-green-effect-hd-wallpapers_jpg";
$o = preg_replace("/_([^_]+)$/", ".\\1", $o);
echo $o;
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 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);