I am looking for some guidance on checking for a string that is returning string(1) " ".
I am rendering a button in a WordPress template file. I first get the post meta, which is added via an API update. I then parse it so as to add the ID to an external booking url.
I need to test to ensure a value is returned of course. However, if I use !empty , this isn't working because even if the field has no value entered, it is still returning string(1) " " from the database.
I could test for strlen, but this doesn't seem very elegant. What would be the best way to test for this?
Many thanks
$s_apply_url0 = get_post_meta( $event_id, '_MyBookingURL', true);
parse_str($s_apply_url0, $output);
$s_apply_url1 = 'https://mybookings.com/event/' . $output ['id'];
if (!empty($s_apply_url0): ?>
<aside id="applybutton-2" class=" grop-side-widget widget_applybutton">
<a href="<?= $s_apply_url1; ?>" class="btn course-apply">
<?= $s_apply_text1 ?>
</a>
</aside>
<?php endif; ?> ```
If I've understood this question correctly, you want to be able to test whether the string is empty or contains more than one space characters
You could first use the trim function of PHP in order to 'trim' the string of whitespace (or, arbitrarily any other characters you wish to determine as 'blank').
<?php
if (empty(trim($myString)) {
// the string is empty, or contains only whitespace
}
If you wish to ONLY test for a single ' ' character, then you could just add that into your conditions, eg:
<?php
if (empty($myString) || $myString === ' ') {
// $myString is empty or only contains a single space character
}
Related
I have a database table that contains these rows, "id" ,"link" and "name"
with link being :
<a href=\"https://www.sample.com/file1.php\">[1-200]< /a> <a href=\"https://www.sample.com/file2.php\">[201-224]< /a>
and name :
item1
I have the following PHP code to get the info from the database and return it to visitors. My problem is that the link for file2.php, is not only applied as hyper link for [201-224], the hyperlink is applied for the rest of the page content also. How do I prevent this? And thanks in advance.
echo "</br> ".$name= $row['name'];
echo "</br> ".$Torrentlink= preg_replace('/\\\\/', '',$row['Torrentlink']);
echo "</br> ";
echo "</br> ";echo "</br> ";
echo "the rest of my text goes here ";
This is a terrible way to handle this type of data. If you know they are all links then you should only be storing the link and the name (of course id and other meta data could be useful). Your current situation allows for too many errors and a maintenance problem for those working behind you. If you do not want to create a record for each link, consider storing them as JSON or some other format.
Example: (Store JSON in DB as VARCHAR)
<?php
//Populate variable from DB
//$TorrentLinks = $row{'Torrentlink'};
$TorrentLinks = '[
{"url":"https://www.sample.com/file1.php","text":"[1-200]"},
{"url":"https://www.sample.com/file2.php","text":"[201-224]"}
]';
//Convert to array
$jsonLinks = json_decode($TorrentLinks,true);
//Iterate and print links
foreach ($jsonLinks as $key => $value) {
echo "{$value["text"]}<br />";
}
Results:
[1-200]
[201-224]
Depending on how you capture the data, you could also use something like htmlspecialchars() to keep the special characters from executing.
I think there's a problem with your preg_replace('/\\\\/', '',$row['Torrentlink']);
/\\\\/ finds text with double backslash \\. It seems that your intention is to find only single backslashes to get rid of them in your links.
So try replacing it with
preg_replace('/\\/', '',$row['Torrentlink']);
For example https://regexr.com/ is a good place to check your regular expressions.
the error was simply the input html text.
the problem was the " < /a> " in the line:
<a href=\"https://www.sample.com/file1.php\">[1-200]< /a> <a href=\"https://www.sample.com/file2.php\">[201-224]< /a>
I had a space before the the backslash.
I am writing an application that will look at a single record, obtain values from about 12 flags (0 or 1), look up those flags against a status table (in MySQL) and return a variable called $status_message which is in that table.
In this table I need to have hyperlinks (working fine) but also echo some variables, i.e.
You have no bids for {{$row->_item_name}}
or
View this item now by clicking here
Now I need item name and the other example to be translated into <?php echo $row->_item_name; ?>
I have tried a preg_replace with the following:
<?php
$find = array('/{{/', '/}}/');
$replace = array('<?php echo ', ' ?>');
echo preg_replace($find, $replace, $status_message);
?>
but this is not working.
Can anyone advise how I can get the desired result and 'echo' the variable in the MySQL field?
Had a brainwave. Much simpler,
instead of $row->_item_name I just put {{itemname}} in the string. I then use the following code:
<?php
$message_buyer = str_replace('{{itemname}}', $row->_item_name , $message_buyer);
echo $message_buyer;
?>
so no need to have <?php calls in the string at all.
Hi I need a simple way with inline PHP to search/replace the URL string, then output the result into the HTML
Our checkout cart redirects people to our post-purchase page, and something like the following ends up in the URL bar:
http://example.com/postpurchasepage.php?amount=$9.99&firstname=Billy&lastname=Bob
More often, based on the browser, they end up with this in their URL bar:
http://example.com?amount=%249.99&firstname=Billy&lastname=Bob
(converts the $ into %24 )
Yes, the cart sends the $ symbol along inside the parameter value. No I don't want it, nor the %24. So, the challenge is to remove both the $ symbol or the %24 (whichever happens),
Right now I am trying to pull the amount value into the page using <?php echo #$_GET['amount']; ?>
For example:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
However that outputs:
<p>Hey Billy !</p>
<p>Thanks for donating %249.99</p>
I want it to output:
<p>Hey Billy !</p>
<p>Thanks for donating 9.99</p>
(or whatever the amount value is that was passed from the cart, without the $ symbol or the %24)
Like I said, basically I am looking for a way to search the URL bar for whatever I define (multiple 'or' type searching), replace with whatever I define, and output the result into the html
Any help appreciated!
You might be looking for urldecode()
echo urldecode("%24"); // $
Then you can strip off the leading $ (if any):
$string = (strpos($string)===0) ? substr($string, 1) : $string;
Manual: http://php.net/manual/en/function.urlencode.php
There are two ways (or more - depending on your creativity & scenario) to get this done;
If you have access to the value of amount before the redirecting occurs, you can strip off the "$" sign and just send in the numeric value and use it there.
Otherwise, you can get the value on the page, and then as willoller mentioned, urldecode it and you can then strip the any leading character that is not numeric. (Not sure if performance would be an issue but that's what worked for me several times before)
Here's a sample for your code:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
<?php
$Get_Amount = urldecode($_GET['amount']);
// WHILE LOOP = TO VALIDATE IF VALUE IS NUMERIC
while (is_numeric($Get_Amount)==false) {
if (is_numeric(substr($Get_Amount, 0,1))){
// DO NOTHING = First Character Numeric
// Might be an issue if you have characters in between the number
// THIS WILL CAUSE RE-LOOP PROBLEM (Dependent on your scenario)
}else{
// TRIM OFF THE FIRST CHARACTER
$Get_Amount = substr($Get_Amount, 1);
}
}
?>
<p>Thanks for donating <?php echo $Get_Amount; ?></p>
Hope this helps! Good Luck!
OR -----------------------
You can use the preg_replace function like this:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
<?php
$String_Get_Amount = urldecode($_GET['amount']);
// Here you can add the characters that you wanna delete
// You should not delete anything that is not numeric cause then it would even replace the decimal point
$String_Search_For_Pattern = '/[$]/';
// Replace it with nothing
$String_To_Replace = '';
$String_Get_Amount = preg_replace($String_Search_For_Pattern, $String_To_Replace, $String_Get_Amount);
?>
<p>Thanks for donating <?php echo $String_Get_Amount; ?></p>
MySql: I have my products table set up as follow:
pg_id | pg_name
1 | Pizza's calzone
2 | Kids menu
Php: Echo out the html while looping through the records in the MySQL table.
<?php do { ?>
<li>
<?php echo "<a href=". "products.php?p_group=" .$row_getproductnames[ 'pg_name'] . ">"; ?>
<?php echo $row_getproductnames[ 'pg_name']; ?>
</a>
</li>
<?php } while ($row_getproductnames=mysql_fetch_assoc($getproductnames)); ?>
My hyperlink: The link to the products.php page should look like this for records with white space in it. This post and reference the product names correctly in the products page.
http://127.0.0.1/products.php?p_group=Pizza's calzone
But it truncates after the white space to
http://127.0.0.1/products.php?p_group=Pizza's
I have checked numerous samples like using in the place of the white space, Html encryption or decryption etc. Still having problem with getting the string to link correctly. Any help would be appreciated.
You need to quote the href with double quotes:
echo "<a href=\"products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "\">"
If you use single quotes or no quotes then the ' in pg_name is misunderstood by the browser.
Your not using quotes? I don't know for sure this is causing it but usually with any parsing issues quotes will fix it.
Try replacing this line:
<?php echo "<a href='products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "'>"; ?>
If you are trying to create a valid URL, you can will want to replace the spaces with a + or %20. Either will do. I also suggest removing the apostrophes:
$new_url = str_replace(" ","+", $old_url); //Replace space
$new_url = str_replace("'","", $new_url ); //Remove apostrophe
Edit:
If you are needing to use the name parameter to retrieve an item from the database, you can do it by 're-replacing' the space and apostrophe characters at the other end like this:
Build the url:
$new_url = str_replace(" ","+", $old_url); //Replace space
$new_url = str_replace("'","APOSTROPHE", $new_url ); //Remove apostrophe
Then at the page where you will perform the SELECT query:
$product_name = str_replace("+"," ", $product_name); //Put spaces back
$product_name = str_replace("APOSTROPHE","'", $product_name ); //put apostrophes back
There are however much easier ways to send values to other pages such as sending a POST request
I'm needing to replace white spacing with a plus sign "+" for the code displayed below.
I'm in the process of modifying some code which generates the label and url for products displayed in my catalog. The problem I face is that my current code doesn't do the replacement. Can someone please modify the code, replacing a spacing for a plus sign "+".
<h5><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h5>
and will return a url something like this:
http://www.efficienttrade.co.nz/catalogsearch/result/?order=relevance&dir=desc&q=potassium nitrate
However, when getName() function is used, names which have a space don't work for the generated search query. So I need to replace the space with a "+" to make the search query url work.
Thanks
As far as i understand your problem, you need to replace spaces by hypens in your product name. This can be achieved by replacing the following code in your href
...<?php echo $this->stripTags($_product->getName(), null, true); ?>...
with
...<?php echo str_replace(' ', '-', $this->stripTags($_product->getName(), null, true)); ?>...
How about the following to make you code slightly nicer (although PHP/HTML soup is never a lot of fun). The first line of PHP is the one that replaces spaces with hyphen
<?php
/*Get product name, stripped of HTML and spaces*/
$productName = str_replace(' ', '-', strip_tags($_product->getName(), null, true));
/*Assign variables rather than using same function multiple times.*/
$productAttribute = $_helper->productAttribute($_product, $_product->getName(), 'name');
/*Concatenate the URL here for easier code fixing later.*/
$url = 'http://www.efficienttrade.co.nz/catalogsearch/result/order=relevance&dir=desc&q=' . $productName;
?>
<h5>
<?php echo $productAttribute ?>
</h5>