easiest way to autolink text and add class to link - html

What would be to the easiest way to do the following?
Random Input:
check out my new video here http://www.youtube.com/watch?v=123abc
or here http://youtu.be/123abc
here is my new wallpaper http://somepage.com/fRDTk.jpg
and my new homepage http://google.com
Output:
check out my new video here
<a class="youtube" href="http://www.youtube.com/watch?v=123abc">
http://www.youtube.com/watch?v=123abc</a>
or here
<a class="youtube" href="http://youtu.be/123abc">
http://youtu.be/123abc</a>
here is my new wallpaper
<a class="image" href="http://somepage.com/fRDTk.jpg">
http://somepage.com/fRDTk.jpg</a>
and my new homepage
<a class="iframe" href="http://google.com">
http://google.com</a>
What is the easiest way to autolink and also add a specific class depending on the link? (image, youtube, other)
I put this together already but its failing miserably.
<?php
foreach ($imgur->captions->item as $comment) {
$co = $comment->caption;
$linkstring = preg_replace('/(http|ftp)?+(s)?:?(\/\/)?+(www.)?((\w|\.)+)+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|be|info|co)+(\/)?(\S+)?/i', '<a rel="fancybox fancybox.iframe" href="\0">\0</a>', $co );
if(preg_match('/^http:\/\/(?:www\.)?(?:youtube.com|youtu.be)\/(?:watch\?(?=.*v=([\w\-]+))(?:\S+)?|([\w\-]+))$/i', $co, $vresult)) {
$pattern = "/(http|ftp)?+(s)?:?(\/\/)?+(www.)?((\w|\.)+)+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|be|info|co)+(\/)?(\S+)?/i";
$replacement = '<a class="fancybox-media" href="\0">\0</a>';
$text = preg_replace($pattern, $replacement, $co);
$type= 'youtube';
}
elseif(preg_match('/(http(s?):)?([\/.|\w|\s])*\.(?:jpg|gif|png|jpeg|bmp)/i', $co, $vresult)) {
$pattern = "/(http|ftp)?+(s)?:?(\/\/)?+(www.)?((\w|\.)+)+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|be|info|co)+(\/)?(\S+)?/i";
$replacement = '<a class="fancybox" href="\0">\0</a>';
$text = preg_replace($pattern, $replacement, $co);
$type= 'image';
}
else {
$pattern = "/(http|ftp)?+(s)?:?(\/\/)?+(www.)?((\w|\.)+)+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU|be|info|co)+(\/)?(\S+)?/i";
$replacement = '<a class="fancybox fancybox.iframe" href="\0">\0</a>';
$text = preg_replace($pattern, $replacement, $co);
$type = 'none';
}
echo "<div class=\"icomment\">
<p class=\"icomment\">“",$text,"” -",$comment->author,"</p>
</div>";
}
?>
I have been messing around with it a lot and just want to scrap it. Hoping there is an easier way to do this.

$patterns = array(
array('pattern'=>'/mypattern/','replaceWith'=>'myreplacement\0','addClass'=>'myclass'),
array('pattern'=>'/mypattern/','replaceWith'=>'myreplacement\0','addClass'=>'myclass'),
);
foreach($patterns as $pattern) {
// .... do your thing here... once... for every possibility described in array above.
}

Related

php xpath query: How to find all <a> in an HTML document that have a title attribute?

php xpath query: How to find all in an HTML document that have a title attribute?
For instance, the following is a sample of all the elements that should be matched. These <a> elements are also nested inside <div>:
<a href="/subdir/22222" title="The title">
Any help would be greatly appreciated.
The query is used in the following code:
$homepage = file_get_contents ("https://somedomain.com");
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
#$doc->loadHTML($homepage);
$xpath = new DOMXpath($doc);
$items = $xpath->query("//a[#title]");
foreach ($items as $node) {
$text = $xpath->evaluate("string(#title)",$node);
$href = $xpath->evaluate("string(#href)",$node);
echo $text;
echo "<br>";
echo $href;
}
//a[#title]
Selects all the a elements in document that have an attribute named title.

Align loop output in one row

I have this code in which the image output $check or $uncheck and title $val_c are inside a loop, what i want is to output everything in one row using <div class="col-sm-12"> but what i always get is a column. what i want is like this
image_title_______________image_title________________image_title
what im getting is like this...
image
title
image
title
image
title
heres is the code
<div class="panel-body">
<div class="col-sm-12">
';
$funcs->viewSurvey();
$array4 = $funcs->viewSurvey();
$searchengine = $array4['searchengine'];
$sum = $array4['sum'];
$searches = explode(',', $searchengine);
$searchesa = array("google","yahoo","bing");
$check = '<img src="images/checked.png" height="40px" width="40px">';
$uncheck = '<img src="images/unchecked.png" height="40px" width="40px">';
foreach ($searchesa as $key_a => $val_c) {
$found = false;
foreach ($searches as $key_b => $val_d) {
if ($val_c == $val_d) {
echo ''.$check.'<h3 class="head8">'. $val_c.'</h3>' ;
$found = true;
}
}
if (!$found)
echo ''.$uncheck.'<h3 class="head8">'. $val_c.'</h3>' ;
}
echo '
</div>
</div>
Use this style
h3 {
display:inline;
}
I already solved it. i just placed the <div class="col-sm-12"> inside the first foreach.

Wordpress: previous_post_link() sits on top, after div "entry-content", no matter where I place it

I have installed a plugin which is custom post type, I am trying to use the previous_post_link() and next_post_link() to get previous and next posts.
It is working fine but the only problem is that I am placing it in my 4th div container where as it always sits on top of page right after div "entry-content".
Is it a known issue?
Here is the piece of code:
function some_function () {
global $post;
if ('team' == get_post_type() && is_single() && in_the_loop()) {
$fields = WPMTPv2_OPTIONS()->fields;
$meta = WPMTPv2_FIELDS($post->ID);
$tmp = '<div id="wpmtp-single-wrap">';
$tmp .= '<div class="wpmtp-vcard">';
$tmp .= '<div class="wpmtp-vcard-left">';
$tmp .= wpmtpv2_featured_img($post->ID);
$previous = previous_post_link('%link', '<div class="wpmtp-gonext">view next</div>');
$next = next_post_link('%link', '<div class="wpmtp-goback">go back</div>');
// $tmp .= '<div class="wpmtp-goback">go back</div>';
// $tmp .= '<div class="wpmtp-gonext">view next</div>';
$tmp .= $previous;
$tmp .= $next;
$tmp .= '</div>';
...........
..........
..........
........
return $tmp;
}
If I place the commented html code instead or previous and next functions, it works fine. Which mean that html and css are right in place, and the problem is in the functions I guess.
And this is what I get after inspecting it in firebug:
<article .....>
<h2 .....></h2>
<div class="meta"></div>
<div class="entry-content">
<a href="link to next" rel="prev">
<div class="wpmtp-gonext">view next</div>
</a>
<a href="link to previous" rel="next">
<div class="wpmtp-goback">go back</div>
</a>
<div id="wpmtp-single-wrap">
<div class="wpmtp-vcard">
<div class="wpmtp-vcard-left">
.....
....
....
....
try this code:
<?php
function some_function () {
global $post;
if ('team' == get_post_type() && is_single() && in_the_loop()) {
$fields = WPMTPv2_OPTIONS()->fields;
$meta = WPMTPv2_FIELDS($post->ID);
?>
<div id="wpmtp-single-wrap">
<div class="wpmtp-vcard">
<div class="wpmtp-vcard-left">
<?php echo wpmtpv2_featured_img($post->ID); ?>
<div class="wpmtp-goback"><?php previous_post_link( '%link', __( ' go back', 'twentyeleven' ),TRUE ); ?></div>
<div class="wpmtp-gonext"><?php next_post_link( '%link', __( 'view next', 'twentyeleven' ),TRUE ); ?></div>
<?php
// replace **twentyeleven** with your theme name
//or try with this line
//previous_post_link('%link', '<div class="wpmtp-gonext">view next</div>');
//next_post_link('%link', '<div class="wpmtp-goback">go back</div>');
?>
</div>
<?php
.........
.........
........
return $tmp;
}
or best to try this plugin : wp-pagenavi
This plugin provides the wp_pagenavi() template tag which generates fancy pagination links.
thanks

How to Display Custom Post Type's Gallery (images ) in Through WP_Query

I am trying to load an Image Slider images from a Custom Post Type image Gallery Feature. For example let say I have an Custom Post Type called banner which I can load images to it's Gallery and I have an HTML code like this:
<div class="item active">
<img src="assets/img/ban1r.jpg" alt="">
<div class="carousel-caption">
slide 1
</div>
</div>
<div class="item active">
<img src="assets/img/ban2r.jpg" alt="">
<div class="carousel-caption">
slide 1
</div>
</div>
Can you please let me know how I can use the custom WP_Query to retrive the images from the custom post and load them into slider instead of above HTML?
So far I have done something like this but it is not displaying any image!
<?php
$args = array( 'post_type' => 'banner');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$gallery = get_post_gallery_images($post);
foreach( $gallery as $image ) {
echo '<div class="item active">';
echo '<img src="' . $image . '">';
echo '</div>';
}
endwhile;
?>
I tried something like this before 2 months ago, and working great for me.
global $wpdb;
$query = "select * from $wpdb->posts
where
post_type = 'banner' and
post_status = 'publish'";
$results = $wpdb->get_results($query, ARRAY_A);
foreach( $results as $result ){
echo '<div id="'.$result['ID'].'" class="listen">';
echo get_the_post_thumbnail( $result['ID'], array(200, 200) );
</div>';
}
Take a look on get_the_post_thumbnail

How to get getRequestUri() in layout.phtml

In my zend layout.phtml I am serving my navigation like this:
<?php
$userInfo = new Zend_Session_Namespace('userInfo');
if($userInfo->userType=='admin')
{
echo '<li >home</li>';
echo '<li >Addbooks</li>';
echo '<li class="selected">EditBook</li>';
echo '<li>Adduser</li>';
echo '<li>Logout</li>';
}
?>
in a normal page I can get url this way
$this->view->assign('url',$this->getRequest()->getRequestUri());
$url = $this->getRequest()->getRequestUri();
$b=basename($url);
$this->view->assign('b',$b);
I want this url in my layout.phtm how can I pass this from bootstrap.php to layout.phtml? here my requirement is to add a class to li like this:
<li <?php if($b==EditBook) echo 'class="selected' ?> ">EditBook</li>
Better way to obtain it, is to write a view helper that will assign necessary variables
class Helper_Params extends Zend_Controller_Action_Helper_Abstract {
$view = $this->getActionController()->view;
$request = $this->getRequest();
$view->requestUri = $request->getRequestUri();
}
and init it in your Bootstrap
public function _initHelpers() {
Zend_Controller_Action_HelperBroker::addHelper ( new Helper_Params () );
}