How to structure php if statement to check for smartquote [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In a db returned record, the var $bookTitle either starts with a smartquote or does not. If the first character is a smartquote, I want to display the record as-is. If the first character is anything else (letter or number), I want the concatenated string to be italicized. I've tried a couple different ways to structure the == '"' without success. The current version is italicized, no matter what is returned (NOTE: the code pasted here won't correctly depict smartquotes).
EDIT: The only part of the statement not working is the ($titleFormat == '"').
EDIT: I discovered that running echo $titleFormat returns a black diamond with a question mark.
<?php
$titleFormat = $bookTitle[0];
if ($titleFormat == '"') {
echo $Parsedown->text($bookTitle . ' ' . $bookSubtitle);
}
else { ?>
<em><?php echo $Parsedown->text($bookTitle . ' ' . $bookSubtitle); ?></em>
<?php
} ?>

Have you tried checking with regex...
if ( preg_match( '/^"|^“/', $bookTitle ) ) {
echo $Parsedown->text( $bookTitle . ' ' . $bookSubtitle );
} else {
printf( '<em>%s</em>', $Parsedown->text( $bookTitle . ' ' . $bookSubtitle ) );
}
Have you also considered that the db result might be encoded, for example: "?
Could it be that the character isn't actually " but “ instead? I've updated the regex above.

Related

How can i insert if statement to variable in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to make a dynamic tab with PHP. please can someone help me out just need to filter where the category parent = 0.
$parent_category_html.= '<span class="'.$current_tab.'"><a id="tab-'
. $category['id'].'-link" href="#tab-'.$category['id']
. '" role="tab" aria-selected="false" aria-controls="tab-1" class="tab-link">'
. **$category['title'] is it possible to put an if statement here to filter the title where parent = 0**
. '</a>
From comments on the question, it sounds like what you're asking is:
How can I conditionally include a value in my string?
For that you'd use the ternary conditional operator. The operation itself would look like this:
$parent == 0 ? $category['title'] : ''
It essentially means:
If $parent equals 0, $category['title'], else empty string
The whole operation resolves to the resulting value, so you can wrap the whole operation in parentheses and drop it anywhere you would a variable. So something like this:
$parent_category_html.= 'your various HTML code...'
. ($parent == 0 ? $category['title'] : '')
. 'the rest of your HTML code...';
You can add if statement before:
if (statement)
$parent_category_html .= '...';
or try to use ternary operator
$parent_category_html .= "<div>" . ($condition ? 'output if true' : 'output if false') . "</div>";
$parent_category_html.= '<span class="'.$current_tab.'"><a id="tab-' . $category['id'].'-link" href="#tab-'.$category['id'] . '" role="tab" aria-selected="false" aria-controls="tab-1" class="tab-link">';
if ($pareent_id ==0) {
$parent_category_html.= '...';
}
$parent_category_html.= '</a>';

How can use tab space in variable [closed]

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 3 years ago.
Improve this question
"\t" is not working on my code
Tried to put a lot on .' '.
$name = '<h4 class="h4">NAME: '."\t".$firstname .' '. $midname .' '. $lastname. '</h4>';
Expected out put is
NAME: Mark stack
Put the Name: inside the <span>Name:</span> and set margin-right to the span to get your desired space.
echo $name = '<h4 class="h4"><span style = "margin-right: 20px;">NAME:</span> '.$firstname .' '. $midname .' '. $lastname. '</h4>';
Demo
html syntax cant read tab.... it dosent know it , as a charachter.
use this. <pre></pre>

Trying to make this entire line of code into a hyperlink [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
echo <a href = 'test.php'> "CategoryID: " . $row["CategoryID"]. " - Category Name: ".$row["CategoryName"]. </a> "<br>";
This is what i have an is not working properly.
This:
echo "<a href = 'test.php'>CategoryID: {$row['CategoryID']} - Category Name: {$row['CategoryName']}</a><br />";
I am using the { and } as they allow you to include an array in a string and ignore the concatenation which I find harder to read.
I find it funny that you can loop through a MySQL array but can't echo a simple string :P
Some links (teach a man to fish...):
W3Schools
PHP documentation
Codecademy
Tutorials Point
Try this:
<?php
$link = "";
$link = sprintf("<a href = 'test.php'>CategoryID: %d - Category Name: %s </a><br />", $row['CategoryID'], $row['CategoryName']);
echo $link;
?>
Assuming that $row['CategoryID'] is an integer and $row['CategoryName'] is a string.

How will make variable to do_shortcode value? i want to change shortcode value like (category="others") how it possible? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to make variables of these red block parts
(screenshot).
PHP code:
echo do_shortcode('[product_category category="others" per_page="12" columns="4"]');
You can directly insert variable values in a "" delimited string in PHP.
$cat = "some_category";
$per = 20; // some number
$col = 10; // some number
echo do_shortcode("[product_category category=\"$cat\" per_page=\"$per\" columns=\"$col\"]");
If you still want to use a '' delimited string, you need to append values together.
echo do_shortcode('[product_category category="' . $cat . '" per_page="' . $per . '" columns="' . $col . '"]');

single quotes vs double quotes, how to arrange? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Please let me know how to writte the below code so as to work because as it is it doesn't work
echo "<a href='$row['url']'>$row['link_text']</a>";
Write so you can read it next time. Also syntax highlight is better this way:
echo '' . $row['link_text'] . '';
You are using ' twice, so you need to escape them or just remove them in this case:
echo "<a href='$row[url]'>$row[link_text]</a>";
When you have to insert complex variables like array values inside strings, usually printf or sprintf is more clear and less error-prone.:
printf("<a href='%s'>%s</a>", $row['url'], $row['link_text']);
This will work:
echo "<a href='".$row['url']."'>".$row['link_text']."</a>";
Also this:
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
It's personal preference.
It's because you've put a ' inside another '.
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
or
echo "<a href='" . $row['url'] . "'>" . $row['link_text'] . "</a>";
Choose the one more to your liking.
You can try with.
echo "<a href='".$row['url']."'>".$row['link_text']."</a>";
Or
echo "<a href='{$row['url']}'>{$row['link_text']}</a>";
Or
echo ''.$row["link_text"].'';

Categories