This code loop returns any number of image links. The variable $picture->descriptionreturns a string of text with underscores that need to be removed.
bluebell_glitter_print_one_piece_with_bubble_skirt_LGG5659WT14_china
All coding variations that I have tried either end in a single correct image link or a white screen.
How can I edit this loop so that the data-caption-desc has no underscores?
if(count($pictures)){
foreach($pictures as $picture){
$output .= '<a href="'.get_option('siteurl').$picture->path.'/'.$picture->filename.'" title="'.$picture->alttext.'" data-caption-title="'.$picture->alttext.'" data-caption-desc="'.$picture->description.'">
<img class="wpnggimgcls" src="'.get_option('siteurl').$picture->path.'/thumbs/thumbs_'.$picture->filename.'" style="" />
</a>';
}
}
You should use str_replace
str_replace('_', ' ', $input_string);
str_replace will replace a characte with another one.
Full:
if(count($pictures)) {
foreach($pictures as $picture){
$output .= '<a
href="'.get_option('siteurl').$picture->path.'/'
.$picture->filename.'" title="'.$picture->alttext.
'" data-caption-title="'.$picture->alttext.'"
data-caption-desc="'
.str_replace('_', ' ',$picture->description).'">
<img
class="wpnggimgcls"
src="'.get_option('siteurl').
$picture->path.'/thumbs/thumbs_'.
$picture >filename.'" style="" />
</a>';
}
}
If you just need to replace the "underscore" into whitespace, you then can use preg_replace(), as follows:
<?php
$strChg='bluebell_glitter_print_one_piece_with_bubble_skirt_LGG5659WT14_china';
echo $strChg;
echo '<br />';
echo '<br />';
$Chg=preg_replace('/_/', ' ', $strChg);
echo $Chg;
?>
So, the point is:
$Chg=preg_replace('/_/', ' ', $strChg);
Related
I have a list of image URLs stored in a database, separated by line breaks. The following PHP code displays them on my site:
$images = explode("\n", $value );
$value = '';
foreach ($images as $im){
$value .= '<img src="'. $im . '" style="max-width:1024px"/> <br />';
}
However, I want to hide some of the images behind a collapsible div if they contain a particular string.
Is it possible to write an if statement so $value changes to some HTML code displaying a collapsible div?
For example:
Rather than $value:
<img src="'. www.example.com/hiddenimage1.jpg . '" style="max-width:1024px"/> <br />
It would become:
<div class="collapsiblediv" style="etc etc"><img src="'. www.example.com/hiddenimage1.jpg . '" style="max-width:1024px"/> <br /></div>
However anything NOT containing "hiddenimage" would display as usual.
Is it possible?
You can use the strstr() function of PHP to search for a chunk of a string within a string.
Using this function, you can search for the value 'hiddenimage' within your foreach loop. Based on a conditional, you could then return either the collapsible <div> or the <img>
You could try this code
$images = explode("\n", $value );
$value = '';
foreach ($images as $im){
if(strstr( $im, 'hiddenimage' ) {
<div class="collapsiblediv" style="etc etc"><img src="'.
$im . '" style="max-width:1024px"/> <br
/></div>
} else {
$value .= '<img src="'. $im . '" style="max-width:1024px"/> <br />';
}
}
I've created a simple function that outputs an image with the correct html markup, seen below. The problem I am facing is when I pass into the alt text that has a space in it, such as "My cat is great", the alt breaks and shows alt="My" like <img src="blah.jpg" alt="My" cat is great class="home">. I'm having a hard time trying to figure this out. Any thoughts?
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
return $string;
}
You're not actually outputting <img src="blah.jpg" alt="My" cat is great class="home"> - that's just how the browser is interpreting it.
You're outputting <img src=blah.jpg alt=My cat is great class=home>
You need to output some quotes:
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
$string = "<img src='".$image_url."' alt='".$alt."' class='".$class."'>';
try this i think it will work
A little user failure. No problem!
Make sure you have your <img> tag correctly.
$image_url = 'https://example.com/image';
$alt = 'My cat is awesome';
$class = 'image';
So:
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
return $string;
}
Outcome:
<img src="https://example.com/image" alt="My" cat="" is="" awesome="" class="image">
Should be:
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
return $string;
}
Outcome:
<img src="https://example.com/image" alt="My cat is awesome" class="image">
Do you see the differences? Look carefully at the quotes.
Documentation:
http://php.net/manual/en/language.types.string.php
Some examples about using quotes:
https://www.virendrachandak.com/techtalk/php-double-quotes-vs-single-quotes/
Personally I would like to use:
// $image below is being sent to the function
$image = [
'image_url' => 'https://example.com/image',
'alt' => 'My cat is awesome',
'class' => 'image',
];
function image_creator($image = [])
{
if(empty($image))
{
return 'No image';
}
return '<img src="' . $image['image_url'] . '" alt="' . $image['alt'] . '" class="' . $image['class'] . '">';
}
//Multiple solution with multiple way
$image_url = 'https://example.com/image';
$alt = 'My cat is awesome';
$class = 'image';
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
//also you can direct echo
echo '<img src="',$image_url,'" alt="',$alt,'" class="',$class,'">';
//also you can direct echo
$string= "<img src='{$image_url}' alt='{$alt}' class='{$class}' >";
//also you can direct echo
$string = "<img src='${image_url}' alt='${alt}' class='${class}' >";
echo $string;
I have PHP variable like:
$biling_cycles = 'Monthly,Annually';
I want it to be:
$biling_cycles = '<span> Monthly, </span> <span> Annually </span>';
You can do it as follows:
$billing_cycles = 'Monthly, Annually';
$temp = '';
foreach (explode(', ', $billing_cycles) as $key => $value) {
$temp .= "<span> $value </span>,";
}
$biling_cycles = rtrim($temp, ','); // removes trainling comma
echo $billing_cycles;
Result is: <span> Monthly </span>,<span> Annually </span>
Follow these simple steps:
$biling_cycles = 'Monthly, Annually';
$cycles = explode(', ', $biling_cycles);
$span_added = "";
foreach($cycles as $cycle){
$span_added = $span_added+"<span>"+$cycle+"</span>, ";
}
echo $span_added;
Output:
<span> Monthly </span>,<span> Annually </span>
The code Below Works for me:
<?php
$biling_cycles = 'Monthly, Annually';
$cycles = explode(', ', $biling_cycles);
foreach($cycles as $cycle):
echo '<span>';
echo $cycle;
echo "</span>";
endforeach;
?>
It is not necessary to explode and iterate.
Just replace all commas with a comma, space, closing tag, space, opening tag, space. Then wrap the whole thing in an openong and closing tag.
Code: (Demo)
$billing_cycles = 'Monthly,Annually';
echo '<span> '
. str_replace(',', ', </span> <span> ', $billing_cycles)
. ' </span>';
I'm having trouble using strpos correctly. if I search for<br /> it will find it. If I search for <br /><br /><br /> with or without space between, it won't and using htmlspecialchars I can tell the string is full of it.
<?php
$picArray = glob('projectData/' . $data['folder'] . '/*.jpg',GLOB_BRACE);
$text = nl2br($data['definition']).'<br />';
$cutP = 0;
foreach($picArray AS $insert) {
if(strpos($text,'<br /> <br /> <br />',$cutP) !== FALSE){
$cutP = strpos($text,'<br /> <br /> <br />',$cutP)+6;
echo $cutP.'_';
$str_to_insert = '<img class="inTextImg" title="int" src="'.$insert.'" />';
$text = substr($text, 0, $cutP) . $str_to_insert . substr($text, $cutP);
}
else {
echo 'haha';
$text .= '<img class="inTextImg" title="outText!" src="'.$insert.'" />';
}
}
?>
Thank your for your ideas.
This is because nl2br keeps the original line break characters in place, just after the '<br />'. You need to include the line break characters in the string to search for. Since there can be a few different patterns for this it's easiest to use a regexp to match it:
$text = preg_replace('/(?:<br \/>\r?\n?){3}/', $str_to_insert, $text);
Have you tried using preg_match() ?
if(preg_match("\(<br />)+\",$text) > 0){
// code
}
Not 100% on the regex, but you would want one that checks for one or more br tags
Can anyone how to implode a image file in breadcrumb in php.
this is my code
function fold_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
if (!empty($breadcrumb)) {
// Use CSS to hide titile .element-invisible.
$output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
// comment below line to hide current page to breadcrumb
$breadcrumb[] = drupal_get_title();
$count= sizeof($breadcrumb);
/* print $count; */
$primarytitle=$breadcrumb[$count-2];
/* print $test; */
$output .= '<nav class="breadcrumb">' . implode(' » ', $breadcrumb) . '</nav>';
return $output;
}
}
Here i need to implode a css image file instead of »
Something like this maybe?
$img_breadcrumb = '<img src="/path/img_bcrumb.png">';
then
$output .= '<nav class="breadcrumb">' . implode($img_breadcrumb, $breadcrumb) . '</nav>';
What about simple analogy:
implode(' <img src="path/to/yourimage.file" /> ', $breadcrumb)
Or better:
implode(' <span class="separator"></span> ', $breadcrumb)
and define CSS background to your .separator class.
This is simple:
implode('<span class="separator"><img src="path/to/imagefile.ext" alt="yourimage.file"/></span>', $breadcrumb);
Hope this helps :)