Content Replacement not in hyperlinks - php

I have a simple function that all content on my website is filtered through to capitilize the company's name. Here is the function:
function brand_capitilize($content) {
$content = str_replace("Brand", "BRAND", $content);
return $content;
}
I need to modify this to ignore the word "Brand" if it's in a url. For example, I do not want the word 'Brand' to be capitalized in the following situation:
View PDF
If I feed this through brand_capitilize() right now, I end up with:
View PDF

Related

Hook to replace a page's content with a string contained in a variable in Wordpress

I'm building a wordpress plugin where I have to render some specific content when appropriated instead of pages.
Here is the current situation (all files are in the root folder of the plugin):
my-template.php
<?php
echo "hi world";
my-plugins-main-php-file.php
//...logic to determine if the content of the page has to be rendered...
function load_template(){
return __DIR__."/my-template.php";
}
add_action('template_include', 'load_template');
And here is the desired result:
my-plugins-main-php-file.php
//...logic to determine if the content of the page has to be rendered...
add_action('string_include_or_some_other_hook',"hi world");
Is there a hook or some other method I can use to render some wordpress content directly from a string?
Thanks!
Here is the solution to my question, thanks to #ArtisticPhoenix:
my-plugins-main-php-file.php
//...logic to determine if the content of the page has to be rendered...
function load_template(){
return __DIR__."/my-template.php";
}
add_action('template_include', 'load_template');
$string = "hi world!";
add_filter('the_content', function() use ($string){
return $string;
});
my-template.php
<?php
the_content();

preg_replace, clickable tags Laravel

If a user writes a post he has the possibility to use hashtags in the text.
the text is saved as body in the post table. A hashtag is a word that starts with a #. The hashtags are stored in a tag table and have a many-to-many relationship to the posts.
if($post)
{
preg_match_all('/#(\w+)/', $request->get('body'),$tagNames);
// $tagnames contains an array of results. $tagnames[0] is all matches
$tagIds = [];
foreach($tagNames[0] as $tagName)
{
//$post->tags()->create(['name'=>$tagName]);
//Or to take care of avoiding duplication of Tag
//you could substitute the above line as
$tag = Tag::firstOrCreate(['name'=>$tagName]);
if($tag)
{
$tagIds[] = $tag->id;
}
}
$post->tags()->sync($tagIds);
}
So I give the data to the view:
$postall = $user->posts()->with('comments', 'tags')->where('status', 1)->latest()->get();
Each hashtag has its own view which can be reached under /tags/id. Now I want to replace the hashtags in the text with an a href element pointing to the respective view. I've read that with preg_replace is possible, but how and where do I have to use it?
Check content for each tag
I assumed you have $post->content and your tags show route's name is tags.show
foreach($post->tags as $tag){
$link = ''.$tag->title.'';
str_replace($tag->title,$link,$post->content);
}
In your view...within the foreach of the content, you can:
{!!str_replace($tag->name,"<a href='your-tag-route/$tag->id'>$tag->name</a>",$tag->name)!!}
Search for $tag->name
Replace with <a href> and your route
Within: $post->content
EDIT 1:
You can replace the tags within the content, the moment they are pulled-in:
In your model:
Post.php
Create a function that will decorate the content of the body attribute by pulling-in the tags, and replacing the content of each occurrence with your pattern.
At your return, the $content that shows on your view will be the redecorated valued.
public function getBodyAttribute ($value){
$tags = Tag::pluck('name', 'id')->toArray();
$replacement = '<a alt="$1" href="/tags/$1">$1</a>';
$pattern = '/\b('.implode('|', $tags).')/i';
return $content = preg_replace($pattern, $replacement, $value);
}
Lastly on your view, use {!! $post->body !!}
EDIT2:
To change and use the Tag name as the key for the table, add this to your Tag model page:
public function getRouteKeyName(){
return 'name';
}
From now on, anytime you Tag::find(), instead of using id you use the name.
For example, Tag::find(php)
You need consistency of the characters (either to always be saved as small letters or big), so, on your Tag model add this:
public function setNameAttribute ($name){
$this->attributes['name'] = strtolower($name);
}
Before saving to the db, it converts all tags to small letters for sake of consistency.
These solve your second problem of calling #du directly.
Note that you dont need to change the replacement code above. I forgot to add #, so this will suffice:
$replacement = '<a alt="$1" href="/tags/$1">#$1</a>';

PHP Define Function without executing

I am currently developing an advertising module for a custom CMS, and using template tags to allow customers to add adverts into their pages through a WSYWIG page content editor.
Eg. {=advert_1}
On the frontend, this will be found through a regex and then be converted into a function, which will look to a database to select and display an advert
Template_tags.php
while ($advertRow = $advertResult->fetch_assoc()) {
$advertGroupID = $advertRow['grpID'];
$advert = "advert_";
${$advert . $advertGroupID} = showAdvert($advertGroupID);
}
This means {=advert_1} will be converted to showAdvert(1)
The problem I am having is that the showAdvert function will be run for all adverts regardless of whether or not it appears on the page, which then adds to the "views", even though the advert may not be displayed.
What I want is to just define the function without executing it, so when it appears in the page content, only then will the function will be executed.
Use a function expression to create a closure.
${$advert . $advertGroupID} = function() use($advertGroupID) {
showAdvert($advertGroupID);
};
To call the function, you need to put parentheses after it:
$name = 'advert_1';
echo $$name();
To use it with preg_replace_callback
preg_replace_callback("/\{=([^\{]{1,100}?)\}/", function($match) {
return $match[1]();
}, $pageContent);

OpenTBS - Content from MySQL as HTML

I have setup OpenTBS to generate dynamically from my contacts database. Now I want to setup the actual content of the email to come from a MySQL table created using CKeditor or another WYSISYG JS editor.
I have been able to get the content to display on the outputted docx file but it is coming this as a straing with the html tags, I somehow need this to format so it comes though as it was put in by the client. I am only going to allow them to use the basics, paragrahs, bold, italics and maybe a few other tags.
Is this someway I can convert this html string to word copy / text? Or somehow have a different WYSIWYG editor to save it in MySQL as word code rather then html code.
Thanks
It's not really relevant but below is the function that generates the OpenTBS document from a template and the database (I am using CakePHP) MailMerge.description is the body with the html code at the moment.
// FUNCTION EXPORT MAIL MERGE
// -------------------------------------------------------------->
function mail_merge()
{
Configure::write('debug',2);
$this->layout = null;
$this->autoRender = FALSE;
// GET THE CONTACTS
// ------------------------------------------------------------->
$contacts = $this->Contact->find('all', array('limit' => 20, 'contain' => FALSE));
$this->loadModel('MailMerge');
$content = $this->MailMerge->find('first', array(
'conditions' => array('MailMerge.id' => 1),
'fields' => array('MailMerge.description')
));
// Get a new instance of TBS with the OpenTBS plug-in
// ------------------------------------------------------------->
$otbs = $this->Tbs->getOpenTbs();
// Load the template file
// ------------------------------------------------------------->
$otbs->LoadTemplate(APP . DS . WEBROOT_DIR . '/files/data_files/enquiry_letter_sample_redcliffe.docx');
// Merge data in the template
// ------------------------------------------------------------->
$otbs->MergeBlock('r', $contacts);
$otbs->MergeBlock('content', $content);
// End the merge and export
// ------------------------------------------------------------->
$file_name = 'export.docx';
$otbs->Show(OPENTBS_DOWNLOAD, $file_name);
}

how to create a content plugin on joomla

I want to create a content plugin on Joomla.
i want to put [picasa id="5551971516356491729"] and that the album will show.
i create a php function that return html for the album.
i will need to had the js code for the light-box.
i have a content plugin that create the light-box can i applay content plugin on another one?
i meen that my plugin will create a code like that [lightbox link="www.example.com/image1.jpg"] and the light-box plugin will generate the html?
Start here http://docs.joomla.org/Creating_a_content_plugin
Your primary function would look something along the lines of...
function onPrepareContent( &$article, &$params, $limitstart )
{
$pattern = '/(\W)[picasa id="([0-9_]+)"](\W)/';
$replacement = '$1#$2$3';
$article->text = preg_replace($pattern, $replacement, $article->text);
return true;
}
You will need to format the new html in a way that your lightbox plugin will detect it and do it's magic.

Categories