I am trying to make a text replacer but since there are letters repeated i keep getting things i dont want. Does anyone know how i can do this?
Input
function image($img) {
$img = ereg_replace("a","<img src=r/a.png>", $img);
$img = ereg_replace("b","<img src=r/b.png>", $img);
$img = ereg_replace("c","<img src=r/c.png>", $img);
return $img;
}
$img = "abc";
echo image($img);
Output
<img sr<img src=r/c.png>=r/a.png><img sr<img src=r/c.png>=r/b.png><img src=r/c.png>
Output I Want
<img src=r/a.png><img src=r/b.png><img src=r/c.png>
Try this, may be insufficient but it will satisfy your requirement:
function image($img) {
$data="";
for( $i = 0; $i <= strlen($img); $i++ ) {
$char =substr( $img, $i, 1 );
switch($char)
{
case 'a':
$data .="<img src=r/a.png>";
break;
case 'b':
$data .="<img src=r/b.png>";
break;
case 'c':
$data .="<img src=r/c.png>";
break;
default:
break;
}
}
return $data;
}
$img = "abc";
echo image($img);
Problem with "c" your "a" and "b" replacing correctly but when it reach "c" there are many "c" cause "src" also added so it replaces all "c"
try with single statement
function image($img) {
$img = ereg_replace("abc","<img src=r/a.png><img src=r/b.png><img src=r/c.png>", $img);
return $img;
}
$img = "abc";
echo image($img);
Also ereg_replace() has been deprecated
Here is what I tried:-
function image($img) {
for($i=0;$i < strlen($img);$i++){
$letterarray[]=$img[$i];
}
$a=0;
foreach ( $letterarray as &$value) { // reference
$value= str_replace($value, "<img src=r/$value.png>", $value);
$a++;
$ab[] = $value;
}
return implode("",$ab);
}
$img = "abc";
echo image($img);
Here in the function image(), you don't need to specify the alphabets contained in $img
Related
Anybody know if there is a way to merge to Paths easily?
/www/htdocs/v450687/server/jobs/bodymind/uploads
uploads/videoscontent/1/
to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/
/www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent
uploads/videoscontent/1/snips
to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/snips
/www/htdocs/v450687/server/jobs/bodymind/uploads/1/1
1/1/snips
to /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1/1/snips
Solution:
built in function to combine overlapping string sequences in php?
echo replaceOverlap("abxcdex", "xcdexfg"); //Result: abxcdexfg
function findOverlap($str1, $str2){
$return = array();
$sl1 = strlen($str1);
$sl2 = strlen($str2);
$max = $sl1>$sl2?$sl2:$sl1;
$i=1;
while($i<=$max){
$s1 = substr($str1, -$i);
$s2 = substr($str2, 0, $i);
if($s1 == $s2){
$return[] = $s1;
}
$i++;
}
if(!empty($return)){
return $return;
}
return false;
}
function replaceOverlap($str1, $str2, $length = "long"){
if($overlap = findOverlap($str1, $str2)){
switch($length){
case "short":
$overlap = $overlap[0];
break;
case "long":
default:
$overlap = $overlap[count($overlap)-1];
break;
}
$str1 = substr($str1, 0, -strlen($overlap));
$str2 = substr($str2, strlen($overlap));
return $str1.$overlap.$str2;
}
return false;
}
As far as I know, there is no built-in method for this. Here is my approach:
Explode the two paths into an array
Merge them together
Remove duplicates.
Here is an example:
$path_1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path_2 = 'uploads/videoscontent/1/';
echo implode('/', array_unique(array_merge(explode('/', $path_1), explode('/', $path_2)), SORT_REGULAR));
Output: /www/htdocs/v450687/server/jobs/bodymind/uploads/videoscontent/1
You have to explode paths into arrays, merge them, and remove duplicates. You can do it in different ways, here are some examples:
$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path2 = 'uploads/videoscontent/1/';
print_r(pathToArray($path1, $path2));
function pathToArray($path1, $path2){
foreach(explode('/', $path1) as $part){
$output1[] = $part;
}
foreach(explode('/', $path2) as $part){
$output2[] = $part;
}
$output = array_merge($output1, $output2);
$output = array_unique($output);
$output = implode("/",$output);
return $output;
}
Or
$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads';
$path2 = 'uploads/videoscontent/1/';
echo implode('/', array_unique(array_merge(explode('/', $path_1), explode('/', $path_2)), SORT_REGULAR));
UPDATE:
As I see you have updated your question, so I develop my answer. In this case to fix this, all you need to do is to use array_unique() for each array instead.
$path1 = '/www/htdocs/v450687/server/jobs/bodymind/uploads/1/1';
$path2 = '1/1/snips';
print_r(pathToArray($path1, $path2));
function pathToArray($path1, $path2){
foreach(explode('/', $path1) as $part){
$output1[] = $part;
}
foreach(explode('/', $path2) as $part){
$output2[] = $part;
}
$output1 = array_unique($output1);
$output2 = array_unique($output2);
$output = array_merge($output1, $output2);
//$output = array_unique($output);
$output = implode("/",$output);
return $output;
}
I need to know if there's a quickest and efficient way to display one or more images if $value == $string
For example: I have an cell which only contains 3 single string: 'r o g', if user put ro it will output <img src="red.gif"> and <img src="orange.gif"> So it could be random if user insert gr then it will display <img src="green.gif"> and <img src="red.gif">
Right now I can only think something like...
<?php $red = "<img src="red.gif">";
$orange = "<img src="orange.gif">";
if( $cell1 == $red ){ echo $red;}
if( $cell1 == $red && $orange ){ echo $orange.$red;}
etc...
This method might works but has to provide too many possibility and I believe there's a shorter and efficient to do that, but I haven't got any idea because still I'm learning PHP
How about this approach?
<?php
//define all your images here
$images = [
'r' => 'red.png',
'g' => 'green.png',
'b' => 'blue.png',
'y' => 'yellow.png',
'o' => 'orange.png'
];
function output($input, $images) {
$parts = str_split($input);
foreach ($parts as $part) {
if (isset($images[$part]))
echo '<img src="' . $images[$part] . '">';
}
}
echo "rgb: \n";
output('rgb', $images);
echo "\n\nyor: \n";
output('yor', $images);
echo "\n\nxxy: \n";
output('xxy', $images);
Output:
rgb:
<img src="red.png"><img src="green.png"><img src="blue.png">
yor:
<img src="yellow.png"><img src="orange.png"><img src="red.png">
xxy:
<img src="yellow.png">
Try this approach:
for( $i = 0; $i <= strlen( $cell1 ); $i++ ) {
$char = substr( $cell1, $i, 1 );
switch ($char) {
case "r":
echo '<img src="red.gif">';
break;
case "o":
echo '<img src="orange.gif">';
break;
case "g":
echo '<img src="green.gif">';
break;
default:
break;
}
}
Here is the code that shows an example for 3 character inputs. $string can be the posted value.
$r = '<img src="red.gif">';
$y = '<img src="yellow.gif">';
$o = '<img src="orange.gif">';
$g = '<img src="green.gif">';
$string = 'ryo';
$length = strlen($string);
for ($i=0; $i<$length; $i++) {
echo ${''.$string[$i]};
}
$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 want to select four random images from a folder which contains a number of images.
What i want to display is a table (2x2) where i can display 4 random distinct images.
Can someone tell me how can i select random distinct files from a folder so that i can store their path in a variable and then can use these variables to display images randomly in the table!
Is there any particular function which can select random files from a folder or something like that?
Use glob() to get all files from directory.
Use array_rand() to get random entries from an array.
<?php
function get_imagess($root) {
$r = array();
$r = array();
$k = glob("$root/*");
shuffle($k);
foreach($k as $n) {
if (is_dir($n)) {
break;
//$r = array_merge($r, get_imagesss($n));
print_r("xxx");
} else {
$r[] = $n;
//print_r($n + "yeah");
}
}
return $r;
}
function get_images($root) {
$r = array();
$k = glob("$root/*");
shuffle($k);
$n = $k[0];
// foreach($k as $n) {
if (is_dir($n)) {
$r = array_merge($r, get_imagess($n));
} else {
$r[] = $n;
}
// }
return $r;
}
$files = get_images('.');
//print_r($files);
//break;
shuffle($files);
$true=true;
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
$ass=0;
while($true)
{
$ass=$ass+1;
if($ass>100){$true=false;}
$imageInfo = pathinfo($files[0]);
if (isset( $extList[ strtolower( $imageInfo['extension'] ) ] )){
$temp = substr($files[0], -5);
$temp = strtolower($temp);
if( $temp == "p.jpg"){shuffle($files);} // checking if jpg has a preview file
else{ // no preview found
$true=false;
//print_r($temp);
}}
else
{
shuffle($files);
//print_r('bad' + $temp);
}
}
//print_r($files[0]);
echo '<HTML><meta HTTP-EQUIV="Refresh" CONTENT="5; URL=./ImageRotateMaybe.php"><img src="';
echo $files[0];
echo ' " width=100%>';
?>
I am new this parsing xml feeds; so I have a piece of code that I have been working on that extracts the data from the xml file. Now I want be able to sort the array by the publish date pubdate. I have not found any examples that can anyone help me.
I used sort() which puts it in some random order.
$xml_headline_key = "*ARRAY*CATCHUP*PROGRAMMENAME";
$xml_link_key = "*ARRAY*CATCHUP*ITEMURL";
$xml_description_key = "*ARRAY*CATCHUP*SHORTSYNOPSIS";
$xml_publish_key = "*ARRAY*CATCHUP*ORIGINALAIRINGDATE";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $description, $link, $pubdate;
}
function contents($parser, $data){
global $current_tag, $xml_headline_key, $xml_link_key, $xml_description_key, $counter, $story_array, $xml_link_key, $xml_publish_key;
switch($current_tag){
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
break;
case $xml_link_key:
$story_array[$counter]->link = $data;
break;
case $xml_description_key:
$story_array[$counter]->description = $data;
$counter++;
break;
case $xml_publish_key:
$story_array[$counter]->pubdate = $data;
break;
}
}
$corrie_counter = 0;
sort($story_array);
$dateformat = "D j M, g:ia";
for($x=0;$x<count($story_array);$x++){
if($story_array[$x]->headline == "Coronation Street"){
if($corrie_counter != 4){
echo "<li><span class=\"bold\">". date($dateformat, strtotime($story_array[$x]->pubdate)) . "</span>\n";
echo "\t<span class=\"text\">" . trunc($story_array[$x]->description,30, " ") . "</span>\n";
echo "\t";
$corrie_counter++;
}
}
}
You can use uasort, and inside sortByPubdate define the way the two pubdates should be compared.
uasort($story_array, 'sortByPubdate');
// Sort function
function sortByPubdate($a, $b) {
if ($a->pubdate == $b->pubdate) {
return 0;
}
return ($a->pubdate < $b->pubdate) ? -1 : 1;
}