I have a PHP function that is calling the following:
function availability_filter_func($availability) {
$replacement = 'Out of Stock - Contact Us';
if(is_single(array(3186,3518)))
$availability['availability'] = str_ireplace('Out of stock', $replacement, $availability['availability']);
}
As you can see I am replacing the text string with custom text, however
I need the "Contact Us" text to be Contact Us - how would I go about doing this? Inputting raw html into the replacement string makes the html a part of the output.
Echo does not work and breaks the PHP function - same with trying to escape the PHP function, inserting html on on the entire string, and unescape the function.
Any advice would be appreciated, thank you.
Your code seems to be a complex way of doing this
function availability_filter_func($availability) {
$default = "Out of Stock";
$string = "<a href='mailto:contact#example.com'>Contact Us</a>";
if($availablity !== "") {
$return = $availability;
} else {
$return = $default;
}
return "$return - $string";
}
Try the Heredoc Syntax
$replacement = <<<LINK
Contact Us
LINK;
Related
I'm trying to make ABBC3 work with PHP 7.3 and PHPBB 3.0.14 since I can't move to PHPBB 3.3 due lots of issues with MODs not ported to extensions and theme (Absolution).
I have asked help in PHPBB forum without luck because 3.0.x and 3.1.x version are not supported anymore.
So after dozens of hours trying to understand bbcode functions I'm almost ready.
My code works when there's a single bbcode in message. But doesn't works when there's more bbcode or it's mixed with texts.
So I would like to get some help to solve this part to make everything work.
In line 98 in includes/bbcode.php this function:
$message = preg_replace($preg['search'], $preg['replace'], $message);
Is returning something like this:
$message = "some text $this->Text_effect_pass('glow', 'red', 'abc') another text. $this->moderator_pass('"fernando"', 'hello!') more text"
For this message:
some text [glow=red]abc[/glow] another text.
[mod="fernando"]hello![/mod] more text
The input for preg_replace above is like this just for context:
"some text [glow=red:mkpanc3g]abc[/glow:mkpanc3g] another text. [mod="fernando":mkpanc3g]hello![/mod:mkpanc3g]"
So basically I have to split this string in valid expressions to apply eval() then concatenate everything. Like this:
$message = "some text". eval($this->Text_effect_pass('glow', 'red', 'abc');) . "another text " . eval($this->moderator_pass('"fernando"', 'hello!');). "more text"
In this specific case there's also double quotes left in '"fernando"'.
I know is not safe apply eval() to user input so I would like to make some type of preg_match and/or preg_split to get values inside of () to pass as parameter to my functions.
The functions are basically:
Text_effect_pass()
moderator_pass()
anchor_pass()
simpleTabs_pass()
I'm thinking in something like this (Please ignore errors here):
if(preg_match("/$this->Text_effect_pass/", $message)
{
then split the string and get value inside of() and remove extra single or double quotes.
after:
$textEffect = Text_effect_pass($value[0], $value[1], $value[2]);
Finally concatenate everything:
$message = $string[0] .$textEffect. $string[1];
}
if(preg_match("/$this->moderator_pass/", $message)
{
.....
}
P.S.: ABBC3 is not compatible with PHP 7.3 due usage of e modifier. I have edited everything to remove the modifier.
Here you can see it working separately:
bbcode 1
bbcode 2
Can someone give me some help please?
After long time searching for a solution for this problem I found this site that helped me build the regex.
Now I have managed to solve the problem and I have my forum fully working with PHPBB 3.14, PHP 7.3 and ABBC3.
My solution is:
// Start Text_effect_pass
$regex = "/(\\$)(this->Text_effect_pass)(\().*?(\')(,)( )(\').*?(\')(,)( )(\').*?(\'\))/is";
if (preg_match_all($regex, $message, $matches)) {
foreach ($matches[0] as $key => $func) {
$bracket = preg_split("/(\\$)(this->Text_effect_pass)/", $func);
$param = explode("', '", $bracket[1]);
$param[0] = substr($param[0], 2);
$param[2] = substr($param[2], 0, strrpos($param[2], "')"));
$effect = $this->Text_effect_pass($param[0], $param[1], $param[2]);
if ($key == 0) {
$init = $message;
} else {
$init = $mess;
}
$mess = str_replace($matches[0][$key], $effect, $init);
}
$message = $mess;
} // End Text_effect_pass
// Start moderator_pass
$regex = "/(\\$)(this->moderator_pass)(\().*?(\')(,).*?(\').*?(\'\))/is";
if (preg_match_all($regex, $message, $matches)) {
foreach ($matches[0] as $key => $func) {
$bracket = "/(\\$)(this->moderator_pass)/";
$bracket = preg_split($bracket, $func);
$param = explode("', '", $bracket[1]);
$param[0] = substr($param[0], 2);
$param[1] = substr($param[1], 0, strrpos($param[1], "')"));
$effect = $this->moderator_pass($param[0], $param[1]);
if ($key == 0) {
$init = $message;
} else {
$init = $mess;
}
$mess = str_replace($matches[0][$key], $effect, $init);
}
$message = $mess;
} // End moderator_pass
If someone is interested can find patch files and instructions here.
Best regards.
I'm programming a bot on telegram and I didn't make the special keyboard via reply_mark up someone can help me?
My code is this:
file_get_contents($website."/sendmessage?chat_id=".$myID."&text=keyTest&reply_markup={"keyboard":[["test"]]}");
If I copy&paste your parameters to my bot and execute the command it works. But that's because I use the Text you provide as parts of my url.
api.telegram.org/bot[key]/sendMessage?chat_id=[id]&text=keyTest&reply_markup={"keyboard":[["test"]]}
What you are doing is writing a script that executes the command. As far as I can tell you're using the dot . to concatenate strings. Another thing you're doing is trying to write the JSON for the reply_markup directly into the url.
What your problem probably is, is one of the following: You're not escaping the " sign or not concatenating variables correctly.
So if keyboard and test are variables you need to concatenate them correctly using the dot:
file_get_contents($website."/sendmessage?chat_id=".$myID."&text=keyTest&reply_markup={".$keyboard.":[[".$test."]]}");
but if you just want to write your test keyboard into the string you need to escape the " so your string does not end:
file_get_contents($website."/sendmessage?chat_id=".$myID."&text=keyTest&reply_markup={\"keyboard\":[[\"test\"]]}");
Note: I have no idea if this is the correct way to escape " in php. This is just to explain your error. If you need to escape double quotes in php any other way, do it how it is supposed to be.
OK, I think that I have the solution for you!
So, this is the code:
$key = "{\"keyboard\":[ [\"OPTION1\"], [\"OPTION2\"], [\"OPTION3\"] ]}";
$url = $GLOBALS[API_URL]."/sendmessage?chat_id=$id&text=Choose%20your%20action&reply_markup=".urlencode($key);
file_get_contents($url);
Variable $GLOBALS[API_URL] = https://api.telegram.org/bot123456789:AAf6g4fr4rt5y67hadsffaerafasfasf
So replace my global var with your direct url or whatever :D
Other function that should be interesting for you is this:
function close_keyboard($id, $message)
{
//$text = "Keyboard_closed!";
$message = str_replace(" ", "%20", $message);
$key = "{\"hide_keyboard\":true}";
$url = $GLOBALS[API_URL]."/sendmessage?chat_id=$id&text=$messagge&reply_markup=".urlencode($key);
file_get_contents($url);
}
This function close your custom keyboard, and other my personal function is this:
function build_keyboard($elements, $message, $chat_id)
{
//Get length of array
$len = count($elements);
//Build custom keyboard
$keyboard = "{\"keyboard\":[ [\"";
for($i = 0; $i < $len; ++$i)
{
if($i < $len-1)
$keyboard .= $elements[$i]."\"],[\"";
else
$keyboard .= $elements[$i]."\"] ]}";
}
$url = $GLOBALS[API_URL]."/sendmessage?chat_id=$chat_id&text=".urlencode($message)."&reply_markup=".urlencode($keyboard);
file_get_contents($url);
}
Prototype of this function is build_keyboard(array(), String, String)
Example:
$messagge = "Wrong choise";
$keyboard = array("OPT1", "OPT2", "OPT3");
build_keyboard($keyboard, $message, $chat_id);
Remember that $message is always needed or reply_doesntt work!
Hope this will be usefull! You're welcome!
So right now, I have a simple function that I use to call some text content:
function htmlstuff() { ?>
<p>html text content here</p>
<? }
And on a page I call the text using:
<?php htmlstuff() ?>
Now, I need to figure out how to use "search and replace" for whatever text is in the function. I've tried things like
function str_replace($search,$replace,htmlstuff())
but I obviously don't know what the heck I'm doing. Is there any simple way to just search the text within the function and search/replace?
what do you really want to do?
if you want to search and replace and in the return variable of your htmlstuff function then
you are not too far away from the correct answer.
function htmlstuff() {
$htmlstuff = "<p>html text content here</p>";
return $htmlstuff;
}
echo htmlstuff();
str_replace($search,$replace,htmlstuff());
this should do the trick
if you just want to make the function htmlstuff more dynamic, then you should take a different approach. something like this:
function htmlstuff($html) {
$htmlstuff = "<p>".$html."</p>";
return $htmlstuff;
}
echo htmlstuff("html text content here");
It would seem like this:
function htmlstuff ($content = "Default text goes here")
{
echo "<p>" . $content . "</p>";
}
and then on another call you just htmlstuff("New text to go there");
And if I'm wrong correct me to solve the problem
<?php
$html_stuff = htmlstuff();
$search_for = "Hello, world!";
$replace_with = "Goodbye, world!";
$html_stuff = str_replace($search_for, $replace_with, $html_stuff);
echo $html_stuff;
function htmlstuff() {
echo '<p>html text content here</p> ';
}
I have searched this problem for a while now, maybe it is simple or maybe not. I could not figure out how to get this to work.
My goal outcome would be a hyperlink related to the post meta with some styling like so.
Check out the r_title here!
The code I have is:
<?php
$rtitle1 = get_post_meta($post->ID, 'r_title', true);
$rlink1 = get_post_meta($post->ID, 'href_link', true);
function testfunction() {
$output .= '<a href=\"'$rlink1'\" style=\"color: #e67300\" rel=\"nofollow\">';
$output .= ' Check out the '$rtitle1' here!</a>';
return $output;
}
add_shortcode('shortcode', 'testfunction');
?>
There are several problems with your code.
The first problem is with string concatenation. When you want to glue strings together you need to use the concatenation operator (the dot: .):
$end = 'a string';
$start = 'This is ';
$string = $start.$end;
If you just juxtapose variables and strings (or any other scalar types) then you will get errors:
$end = 'a string';
$string = "This is "$end; // Error!
The second problem is that you are using two variables ($rtitle1 and $rlink1) that are in the global scope. If you want to use global variables inside a function then you need to declare them as global inside the function:
$globalVar = 'test';
function test() {
global $globalVar;
echo $globalVar;
}
The third problem is that you forgot the ending closing parenthesis, ), for the get_post_meta() function:
$rtitle1 = get_post_meta($post->ID, 'r_title', true;
$rlink1 = get_post_meta($post->ID, 'href_link', true;
They should be like this:
$rtitle1 = get_post_meta($post->ID, 'r_title', true);
$rlink1 = get_post_meta($post->ID, 'href_link', true);
Before you think about asking for help you should look at the error messages that you get. If you have not seen the error message before then Google it. The best way to learn something is to find the solution on your own. Asking questions is for when you have tried finding a solution but you cannot find it.
I have a function that will add the <a href> tag before a link and </a> after the link. However, it breaks for some webpages. How would you improve this function? Thanks!
function processString($s)
{
// check if there is a link
if(preg_match("/http:\/\//",$s))
{
print preg_match("/http:\/\//",$s);
$startUrl = stripos($s,"http://");
// if the link is in between text
if(stripos($s," ",$startUrl)){
$endUrl = stripos($s," ",$startUrl);
}
// if link is at the end of string
else {$endUrl = strlen($s);}
$beforeUrl = substr($s,0,$startUrl);
$url = substr($s,$startUrl,$endUrl-$startUrl);
$afterUrl = substr($s,$endUrl);
$newString = $beforeUrl."".$url."".$afterUrl;
return $newString;
}
return $s;
}
function processString($s) {
return preg_replace('/https?:\/\/[\w\-\.!~#?&=+\*\'"(),\/]+/','$0',$s);
}
It breaks for all URLs that contain "special" HTML characters. To be safe, pass the three string components through htmlspecialchars() before concatenating them together (unless you want to allow HTML outside the URL).
function processString($s){
return preg_replace('#((https?://)?([-\w]+\.[-\w\.]+)+\w(:\d+)?(/([-\w/_\.]*(\?\S+)?)?)*)#', '$1', $s);
}
Found it here