Get content between shortcodes - php

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.

Related

How to change place of text in echo?

I'm setting up a script for a friend, I want the landing_{random} to be added after the domain names but I whatever I do it shows behind the URL. How can I fix this issue?
I have tried moving the variables around and reversing places with the variables.
<?php
$lines = file('domains.txt');
foreach($lines as $line) {
$randomString = substr(str_shuffle("0123456789abcdef"), 0, 1) .
substr(str_shuffle("0123456789abcdef"), 0, 6);
$landing = "/landing_$randomString";
/*echo "$line.'/landing_'.$randomString";*/
echo "$line{$landing}";
}
?>
I want when I input URL in http://example.com in domains.txt for it to output http://example.com/landing_d2ae5b3
It will not work like that Try instead:
$landing = '/landing_".$randomString."';
Should work fine now
Update answer
Try this
$landing = "/landing_".$randomString."";
By the way I think you may need to define array before use it like
$lines = explode("" ,$lines);
//then
$landing = "/landing_".$randomString."";
Also you may use this :
echo "$line".$landing."";

php preg_replace reuse of subject

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);
}

Alternatives to eval() - Shortcodes in text input

in a CMS I want the user to be able to include blocks of php code like a contact form, a search form.
Something like
<? include 'contact.inc.php' ?>
Now, Wordpress does this by using Shortcodes, ie
[contact-form] [search form]
How can I do this easily?
I have a limited number of includes
I could use
eval('?> ' . $database_query . ' <?php ');
and put
<? include('contact.inc.php') ; ?>
in the contents of $database_query
but eval() in this situation is dangerous
so how can I make a simple shortcode system ?
I would avoid eval all together if you can. The issue with eval, particularly with form inputs is that if exploited, you are giving an attacker access to your entire web server. I would suggest using some server-side script that is looking for explicit shortcodes. You can even use the Wordpress function that they use as a starting point. You can also use a preg_match to search for your shortcodes in the text and parse them that way. Either option would be better than eval()
Assuming you've loaded some HTML with your shortcodes from somewhere (database for example) and you want to echo it's contents with your forms inside, I would do the following:
// Define known shortcodes
$shortcodes = array(
"[contact-form]" => "contact.inc.php",
"[search-form]" => "search.inc.php"
);
// Load HTML with shortcodes from wherever you want
$source = load_template_from_somewhere();
// Now let's find the shortcodes in your source
$replace_points = array();
foreach($shortcodes as $code => $include_me){
$offset = 0;
while(TRUE){
$index = strpos($source, $code, $offset);
if($index === FALSE){
// No more shortcodes of this type found
break;
};
// Save the position and name of shortcode
$replace_points[$index] = $code;
// Update offset to scan the source search the remaining part of the string
$offset = $index + strlen($code);
};
};
// Sort the array because we've been searching by shortcode names
// And now we need to include forms in the correct order
ksort($replace_points);
$offset = 0;
foreach($replace_points as $index => $code){
// Echo raw HTML part of the string
echo substr($source, $offset, $index);
// Then include the form
include($shortcodes[$code]);
// Update the offset to move towards the end of the string
$offset = $index + strlen($code);
};
// Echo the remaining part of raw HTML string
echo substr($source, $offset);

Get specific part of a URL value

I used a code to bulk import the envato items,
for example the envato items url is like this :
http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226
How to get only the part "2833226" of this URL with PHP?
I use wordpress and have used custom fields to insert envato item links
for example a custom field (afflink) with value :
http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226
and this code to call the custom field value in the theme
$afflink = get_post_meta($post->ID, "afflink", false);
How to get only the item number?
Use explode to split using / caracter and then get the last element of array:
<?php
$url='http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226';
$y=explode('/',$url);
$affiliateID = end($y);
?>
$pattern = "/\d+$/";
$input = "http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226";
preg_match($pattern, $input, $matches);
//Your ID
$post_id = $matches[0];
Use this :
value = window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
$url = 'http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226?r=1';
$urlParts = parse_url($url);
$path = $urlParts['path'];
$pathParts = explode('/',$path);
$item_id = end($pathParts);
The above code will make sure to avoid query string and read only Uri

How to write a PHP script to find the number of indexed pages in Google?

I need to find the number of indexed pages in google for a specific domain name, how do we do that through a PHP script?
So,
foreach ($allresponseresults as $responseresult)
{
$result[] = array(
'url' => $responseresult['url'],
'title' => $responseresult['title'],
'abstract' => $responseresult['content'],
);
}
what do i add for the estimated number of results and how do i do that?
i know it is (estimatedResultCount) but how do i add that? and i call the title for example this way: $result['title'] so how to get the number and how to print the number?
Thank you :)
I think it would be nicer to Google to use their RESTful Search API. See this URL for an example call:
http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:stackoverflow.com&filter=0
(You're interested in the estimatedResultCount value)
In PHP you can use file_get_contents to get the data and json_decode to parse it.
You can find documentation here:
http://code.google.com/apis/ajaxsearch/documentation/#fonje
Example
Warning: The following code does not have any kind of error checking on the response!
function getGoogleCount($domain) {
$content = file_get_contents('http://ajax.googleapis.com/ajax/services/' .
'search/web?v=1.0&filter=0&q=site:' . urlencode($domain));
$data = json_decode($content);
return intval($data->responseData->cursor->estimatedResultCount);
}
echo getGoogleCount('stackoverflow.com');
You'd load http://www.google.com/search?q=domaingoeshere.com with cURL and then parse the file looking for the results <p id="resultStats" bit.
You'd have the resulting html stored in a variable $html and then say something like
$arr = explode('<p id="resultStats"'>, $html);
$bottom = $arr[1];
$middle = explode('</p>', $bottom);
Please note that this is untested and a very rough example. You'd be better off parsing the html with a dedicated parser or matching the line with regular expressions.
google ajax api estimatedResultCount values doesn't give the right value.
And trying to parse html result is not a good way because google blocks after several search.
Count the number of results for site:yourdomainhere.com - stackoverflow.com has about 830k
// This will give you the count what you see on search result on web page,
//this code will give you the HTML content from file_get_contents
header('Content-Type: text/plain');
$url = "https://www.google.com/search?q=your url";
$html = file_get_contents($url);
if (FALSE === $html) {
throw new Exception(sprintf('Failed to open HTTP URL "%s".', $url));
}
$arr = explode('<div class="sd" id="resultStats">', $html);
$bottom = $arr[1];
$middle = explode('</div>', $bottom);
echo $middle[0];
Output:
About 8,130 results
//vKj
Case 2: you can also use google api, but its count is different:
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=ursitename&callback=processResults
https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=site:google.com
cursor":{"resultCount":"111,000,000","
"estimatedResultCount":"111000000",

Categories