I've got a big problem and hopefully you can help me out...
I'd like to code a plugin. The plugin should search on a website (for example www.example.com/index.php for a specific word for example Number1. Afterwords the word found should be replaced by an hyperlink. The link should guide to an extern website for example www.example2.com/index.php.
According to the current website, the found word should added to the hyperlink. So if the current site is www.example.com/index.php and the found word is Number1, the hyperlink should look like this:
www.example2.com/index.php/Number1
And it shouldn't be always the same hyperlink, but created dynamic by the word this plugin finds.
Beneath is my current code.
I hope someone can help me out. Thanks.
public function onContentPrepare($context, &$row, &$params, $page = 0)
{
$text = $row->text;
$pattern = array();
$pattern[0] = '/Number1/';
$pattern[1] = '/Number2/';
$pattern[2] = '/Number3/';
$Subject = array();
$Subject[2] = 'https://www.example.com/index.php/';
$Subject[1] = '(...)';
$Subject[0] = '(...)';
$row->text = preg_replace($pattern,$subject,$text);
}
Related
The task itself is for the Wordpress widget.
In each post there are links like "https://www.test.com/example/example1/target", and the task is as follows: find the first link in text leading to "https://www.test.com/example/example1/ "and create a variable that takes the value of "target". Is it real to do? If so, how? I will be very grateful for help. Sorry for my English!
UPD:
I've done this (using $company_ticker as a "target")
$patt = '~(https://www.test.com/example/example/)(\S+)~';
$company_ticker = '';
if (preg_match($patt, $post_content, $company_ticker)) {
list( , $url, $company_ticker) = $company_ticker;
}
But I can't get where is the mistake...
I'm trying to append my variable that displays the count after my links, but I already have code I need replacing links to redirect them to a redirect page.
This is my code currently:
$this->post['message'] = str_replace('href="', 'href="./out.php?link=', $this->post['message']);
$this->post['signature'] = str_replace('href="', 'href="./out.php?link=', $this->post['signature']);
So all the links get redirected to out.php
(small additional question how can i make this not effect img href's?)
So back on topic, what I'm trying to do is add $someVariable[value] after my links, also not effecting images.
The variable would show plain text (numbers)
and for example the end result would look like:
Google(##)
where ## represents $someVariable
DOM confuses me, and I cant think of any other way to do it, so I need some help.
1
$ur = "./out.php";
$k = str_shuffle("7dfg9bf09cv04e79507e197a1dtdy8j3");
$web = $ur."/?link=".$k;
echo $web;
Here is an example.
2
Escaping quotation marks in PHP
<?php
$ur = "./out.php";
// $k = $this->post['message'];
$k = "hello";
$web = "/?link=".$k;
$li = "".$k."(".$k.")";
echo $li."\n";
?>
I need to get the content between a wordpress shortcode.
Like say [bla_blabla name="a"]Variation A[/bla_blabla]
should return Variation A
I thought of using regex to get it, but there is a shortcodes.php file in wordpress which should already be capable of doing it? Could you guide me into the correct way of doing it?
I have currently used this code
<?php
function abtest_runner($input) {
//A bit confused if you wanted equal chances of selecting one or equal changes?
//This is for equal chances.
$size = count($input);
$select = rand(0,$size-1);
$temp=array();
$selection = $input[$select];
$temp = (explode("]",$selection));
$temp = (explode("[",$temp[1]));
echo $temp[0];
}
$input[0] = '[bla_blabla name="a"]Variation A[/bla_blabla]';
$input[1] = '[bla_blabla name="b"]Variation B [/bla_blabla]';
abtest_runner($input);
?>
If you have registered you shortcode in WordPress using the [add_shortcode][1] function then, you can extract your shortcode content using the following method:
$pattern = get_shortcode_regex();
$content = '[bla_blabla name="a"]Variation A[/bla_blabla]';
$matches = array();
preg_match("/$pattern/s", $content, $matches);
print_r($matches[5]);
$matches[5] will contain your content.
Hope this helps.
I am making a system with a email function but I want that the admin can change those email messages with the admin panel. I am planning to do this with ckeditor but I want that I can add tags where for example the user name will be placed. Something like this:
Dear %%name%%,
Thank you for purchasing %%item%%.
Kind Regards,
%%business%%
Thanks !
A part of your code could look like this:
$text = <text from editor>
$tokens['name'] = 'Mr. Jones';
$tokens['item'] = 'left shoe';
$tokens['business'] = 'WalkThisWay';
// match anything between '%%' than's not a control chr or '%', min 3 and max 16 chrs.
if (preg_match_all('/%%[^[:cntrl:]%]{3,16}%%/',$text,$matches))
{
// replace all found tokens
foreach ($matches[0] as $match)
{
// get token
$tokenKey = trim($match,'%');
// replace
$text = str_replace($match,$tokens[$tokenKey],$text);
}
}
It seeks out matching tokens and replaces them with the real content. You still need to think about how to get your $tokens array.
K2 is parsing un-necessary text into urls in item comments.
1.Created a item using joomla admin panel and as a guest entered comment with following text
"node.js is a power full js engine. Enven.though this is not a valid url it has been rendered as valid.url anything with xxx.xxx are parsed as urls and even like sub domain syntax iam.not.valid i.e mail.yahoo.com how funny this is"
In the above coomment node.js, even.though, valid.url, xxx.xxx iam.not.valid i.e mail.yahoo.com are rendered as valid url. but in this case only mail.yahoo.com is valid not others.
K2 is using some smart intelligence using following snippet in $JHOME/components/com_k2/views/item/view.html.php lines (159-178)
$comments = $model->getItemComments($item->id, $limitstart, $limit, $commentsPublished);
$pattern = "#\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\#)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\#&=+$,%#-]+)*/?)#";
for ($i = 0; $i < sizeof($comments); $i++) {
$comments[$i]->commentText = nl2br($comments[$i]->commentText);
$comments[$i]->commentText = preg_replace($pattern, '<a target="_blank" rel="nofollow" href="\0">\0</a>', $comments[$i]->commentText);
$comments[$i]->userImage = K2HelperUtilities::getAvatar($comments[$i]->userID, $comments[$i]->commentEmail, $params->get('commenterImgWidth'));
if ($comments[$i]->userID>0) {
$comments[$i]->userLink = K2HelperRoute::getUserRoute($comments[$i]->userID);
}
else {
$comments[$i]->userLink = $comments[$i]->commentURL;
}
if($reportSpammerFlag && $comments[$i]->userID>0) {
$comments[$i]->reportUserLink = JRoute::_('index.php?option=com_k2&view=comments&task=reportSpammer&id='.$comments[$i]->userID.'&format=raw');
}
else {
$comments[$i]->reportUserLink = false;
}
}
Can somebody help fixing above regular expression? Thanks
You are going to have this problem any time a user types.in a period with no spaces around it. You could add in some login to test for valid TLDs, but even that would not be perfect because there are plenty of TLDs that would fool the logic, like .it.
If you want to try your hand at fixing the regular expression, the pattern that determines if a string is a URL is here -
$pattern = "#\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\#)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\#&=+$,%#-]+)*/?)#";
Personally, I would just disable links in comments altogether by removing or commenting out this code -
$comments[$i]->commentText = preg_replace($pattern, '<a target="_blank" rel="nofollow" href="\0">\0</a>', $comments[$i]->commentText);