I've got a string like this:
$foo = 'height:200px;color:#CCC;font-size:12px';
Can I somehow double the font-size amount in the string?
Try with:
$foo = 'height:200px;color:#CCC;font-size:12px';
$key = 'font-size:';
preg_match('/' . $key . '(\d+)/', $foo, $matches);
$size = (int) $matches[1];
$foo = str_replace($key . $size, $key . ($size * 2), $foo);
can you please used this code :
<?php
$foo = 'height:200px;color:#CCC;font-size:12px';
$styleArr = explode(";",$foo);
$newStyleArr = array();
foreach ($styleArr as $style) {
$explode_str = explode(":",$style);
if($explode_str[0]=="font-size" && count($explode_str)>0) {
$explode_str[1] = str_replace("px","",$explode_str[1]);
$explode_str[1] = ($explode_str[1]*2)."px;";
}
$newStyleArr[] = implode(":",$explode_str);
}
echo implode(";",$newStyleArr);
?>
OUTPUT :
height:200px;color:#CCC;font-size:24px;
Related
I have a main array containing the following arrays
(at least 50 different types) :
I'm currently programming in php but i can probably adapt the same functions in java , c# etc...
array('type'=>'x-y','value'=>'12');
array('type'=>'y-z','value'=>'6');
array('type'=>'y-x','value'=>'56');
array('type'=>'z-x','value'=>'19');
array('type'=>'z-y','value'=>'18');
array('type'=>'x-z','value'=>'67');
........
I want to create and traverse a graph for each key and run operations on them e.g :
x-y-z-x : 12 * 6 * 19
x-y-x : 12 * 56
x-z-x : 67 * 19
y-z-y : 6 * 18
.......
I have been trying to run each independently using foreach loops but it won't work for the size of dataset that i have .
Any help would be greatly appreciated. Thanks
The code is in php as required, you can test it as well..
function initData($final, $paths, $values) {
for($i=0; $i<sizeof($final);$i++) {
$path = explode('-', $final[$i]["type"]);
$value = $final[$i]["value"];
array_push($paths, $path);
array_push($values, $value);
}
return [$paths, $values];
}
function getFinalValue($finalPath, $paths, $values) {
$path = explode('-', $finalPath);
$finalValue = 1;
for($i=0;$i<sizeof($path)-1;$i++) {
$stop = $path[$i+1];
$start = $path[$i];
$smallPath = array($start, $stop);
$finalValue *= getValue($smallPath, $paths, $values);
}
return $finalValue;
}
function getValue($smallPath, $paths, $values) {
$value = 0;
for($i=0; $i<sizeof($paths);$i++) {
if($smallPath[0] == $paths[$i][0])
{
if($smallPath[1] == $paths[$i][1]) {
print_r($smallPath);
echo ' ';
$value = $values[$i];
print_r($value);
echo '<br>';
}
}
}
return $value;
}
//Test
function test() {
$a = array('type'=>'x-y','value'=>'12');
$b = array('type'=>'y-z','value'=>'6');
$c = array('type'=>'y-x','value'=>'56');
$d = array('type'=>'z-x','value'=>'19');
$e = array('type'=>'z-y','value'=>'18');
$f = array('type'=>'x-z','value'=>'67');
$paths = [];
$values = [];
$testString = 'x-y-z';
$final = array($a,$b,$c,$d,$e,$f);
$data = initData($final, $paths, $values);
$paths = $data[0];
$values = $data[1];
for ($i=0;$i<sizeof($paths);$i++) {
print_r($paths[$i]);
print_r($values[$i]);
echo '<br>';
}
echo '<br>';
echo '<br>';
$finalValue = getFinalValue($testString, $paths, $values);
echo $testString . '=' . $finalValue;
}
test();
?>
I want to cut text in array but I have no idea to cut this
I try strstr() but it not true.
I try
$ff='';
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$ff .= $row['fav'] . ",";
}
if( strpos( $ff, "_" )) {
$text = strstr($ff, '_');
echo $text;
}
$ff ='A_0089,A_5677,B_4387,A_B_5566,'
I want output show
0089,5677,4387,B_5566,
Here is one example, using substr() with strpos():
$ff='A_0089,A_5677,B_4387,A_B_5566';
$items = explode(',', $ff);
foreach($items as $item) {
echo substr($item, strpos($item, '_')) . "\n";
}
The above code returns:
_0089
_5677
_4387
_B_5566
You're better off not building a string, but building an array. The way you build the string you have a dangling comma, which you do not want.
$ff = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$ff[] = $row['fav'];
}
foreach($ff as $item) {
echo substr($item, strpos($item, '_')) . "\n";
}
Based on your desire to keep the commas and create a string:
$ff='A_0089,A_5677,B_4387,A_B_5566,';
$items = explode(',', $ff);
foreach($items as $item) {
$new[] = substr($item, strpos($item, '_'));
}
$newFF = implode(',', $new);
echo $newFF;
returns:
_0089,_5677,_4387,_B_5566,
Probably this is what you are looking for
<?php
function test_alter(&$item1)
{
$pattern = '/^[A-Z]{1}[_]{1}/';
$item1 =preg_replace($pattern,"",$item1);
}
$ff="A_0089,A_5677,B_4387,A_B_5566,";
$nff=explode(",",$ff);
array_walk($nff, 'test_alter');
echo implode(",",$nff);
?>
I'm making a json string which only contains the values which are entered in the form. so far it's going good but the only problem is that the foreach function which makes the string does not take all items from array.
Let's explain something and show my code:
if the $title is not empty I want my final ($content1) string to start with that value. Because it has some other quotes and curlybraces than other values. the same with the date field I also made an if for that because it also contains "date" in it. See my code below:
$title = "fiat titel";
$engine = "1.8";
$day = "1";
$month = "2";
$year = "1999";
$dateBuild = "02/02/1999";
// assoc array push function
function array_push_assoc($array, $key, $value){
$array[$key] = $value;
return $array;
}
$array = array();
if(!empty($title)) {
$array = array_push_assoc($array, 'title', $title);
}
if(!empty($color)) {
$array = array_push_assoc($array, 'field_modelcolor', $color);
}
if(!empty($model)) {
$array = array_push_assoc($array, 'field_carmodel', $model);
}
if(!empty($engine)) {
$array = array_push_assoc($array, 'field_modelengine', $engine);
}
if(!empty($fuel)) {
$array = array_push_assoc($array, 'field_modelfuel', $fuel);
}
if(!empty($day) && !empty($month) && !empty($year)) {
$array = array_push_assoc($array, 'field_modelyear', $dateBuild);
}
$content1 = "";
foreach($array as $ftitle => $value) {
if(empty($content1) && !empty($title)) {
$content1 = ',"' . $ftitle . '":"' . $value . '"';
}
if(!empty($day) && !empty($month) && !empty($year)) {
$content1 = ',"' . $ftitle . '":{"und":[{"value":{"date":"' . $value . '"}}]}';
}
$content1 = ',"' . $ftitle . '":{"und"[{"value":"' . $value . '"}]}';
}
$postContent = '{"type":"carmodels"' . $content1 . '}';
var_dump($array);
echo $postContent;
Right now it's only echo'ing:
{"type":"carmodels","field_modelyear":{"und"[{"value":"02/02/1999"}]}}
And it should be (example):
{"type":"carmodels","field_modelyear":{"und":[{"value":{"date":"02/02/1999"}}]},"field_modelengine":{"und"[{"value":"1.6"}]}}
Thanks in advance!
$variable = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf";
I need to display above the $variable like
Test Company Insurance LLC Chennai Limited W-8TYU.pdf
For that I've done:
$variable = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");
$test = explode(" ", $variable);
$countof = count($test);
for ($x=0; $x<$countof; $x++) {
if($test[$x] == 'w-8tyu' || $test[$x] == 'llc') {
$test[$x] = strtoupper($test[$x]);
//todo
}
}
I've got stuck in the to-do part.
I will change the specific words to uppercase using strtoupper.
Later, how should I need to merge the array?
Any help will be thankful...
$str_in = "test_company_insurance_llc_chennai_limited_w-8tyu.pdf";
$lst_in = explode("_", $str_in);
$lst_out = array();
foreach ($lst_in as $val) {
switch($val) {
case "llc" : $lst_out[] = strtoupper($val);
break;
case "w-8tyu.pdf" : $lst_temp = explode('.', $val);
$lst_out[] = strtoupper($lst_temp[0]) . "." . $lst_temp[1];
break;
default : $lst_out[] = ucfirst($val);
}
}
$str_out = implode(' ', $lst_out);
echo $str_out;
Not terribly elegant, but perhaps slightly more flexible.
$v = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");
$acronyms = array('llc', 'w-8tyu');
$ignores = array('pdf');
$v = preg_replace_callback('/(?:[^\._\s]+)/', function ($match) use ($acronyms, $ignores) {
if (in_array($match[0], $ignores)) {
return $match[0];
}
return in_array($match[0], $acronyms) ? strtoupper($match[0]) : ucfirst($match[0]);
}, $v);
echo $v;
The ignores can be removed provided you separate the extension from the initial value.
See the code below. I have printed the output of the code as your expected one. So Run it and reply me...
$variable = str_replace("_"," ","test_company_insurance_llc_chennai_limited_w-8tyu.pdf");
$test = explode(" ", $variable);
$countof = count($test);
for ($x=0; $x<$countof; $x++) {
if($test[$x] == 'llc') {
$test[$x] = strtoupper($test[$x]);
//todo
}elseif($test[$x] == 'w-8tyu.pdf'){
$file=basename($test[$x],'pdf');
$info = new SplFileInfo($test[$x]);
$test[$x] = strtoupper($file).$info->getExtension();
}
else{
$test[$x]=ucfirst($test[$x]);
}
}
echo '<pre>';
print_r($test);
echo '</pre>';
echo $output = implode(" ", $test);
I have a string in PHP like the following
$data = "ID=53KEY=23";
and i want to assign value from this string to following variables
$id = 53;
$key = 23;
How can i do this in php, please help?
This function will work for more generic key/value inputs, not just ID/KEY
$input = "ID=53KEY=23";
$res = preg_split("/([[:upper:]]+)=([[:digit:]]+)/", $input, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
for ($i = 0 ; $i < count($res) ; $i += 2)
{
$res[$i] = strtolower($res[$i]);
$$res[$i] = $res[$i+1];
}
//$id = 53
//$key = 23
For a more generic solution:
$data = "ID=53KEY=23AGE=318";
$array = array();
if(preg_match_all("/([A-Z]+)=(\d+)/", $data, $matches)) {
$array = array_change_key_case(array_combine($matches[1], $matches[2]));
}
echo "ID: " . $array['id'] . ", KEY: " . $array['key'] . ", AGE: " . $array['age'];
Try this:
$data = "ID=53KEY=23";
preg_match("/id=(?<id>\d+)&?key=(?<key>\d+)/i",$data,$array);
$id = $array["id"]; // 53
$key = $array["key"]; //23
print("id = $id, key = $key\n");