Find URL in string, replace it and alter href - php

I've currently got a few DB entries which look like this:
1. This is some text http://www.sitehere.com more text
2. Text https://www.anothersite.com text text text
3. http://sitehere.com http://sitehereagain.com
4. Just text here blabla
I am trying to filter those entries while printing them and add infront of all the urls http://anothersite.com/?. Also put the new url destination as link but keep the original url as text:
text text http://sitehere.com text
Until now I've managed to add the http://anothersite.com/? part with the following code:
$result = preg_replace('/\bhttp:\/\/\b/i', 'http://anothersite.com/?http://', $input);
$result = preg_replace('/\bhttps:\/\/\b/i', 'http://anothersite.com/?https://', $input);
But the ahref is not the way I want it. Instead it is:
text text http://anothersite.com/?http://sitehere.com text
PS: I am not looking for a javascript solution :) Thank you!

This following code should work. There are a few large changes I made. The first one is I am using preg_replace_callback instead of preg_replace so I am able to properly encode the URL and have more control over the output. The other change is I'm matching the whole domain so the callback function can insert the URL between the <a> tags and also can add it to the hyperlink.
<?php
$strings = array(
'This is some text http://www.sitehere.com more text',
'Text https://www.anothersite.com text text text',
'http://sitehere.com http://sitehereagain.com',
'Just text here blabla'
);
foreach($strings as $string) {
echo preg_replace_callback("/\b(http(s)?:\/\/[^\s]+)\b/i","updateURL",$string);
echo "\n\n";
}
function updateURL($matches) {
$url = "http://anothersite.com/?url=";
return ''.$matches[1].'';
}
?>

Related

basic php 'if then' URL link in wordpress

I'm trying to write code that will display a URL link if there one present from a form sumbmission;
If > [a link exists]
then [display the text 'more info' with the href link wrapped around it]
I've confused myself mixing wordpress and php, and can't quite get it. Any help would be great.
This question isn't very specific, but the pseudo-code I can offer is this:
<?php if (isset($_GET['url'])): ?>
Read more
<?php endif; ?>
Are you looking to do something like this to the comments displayed on a post?
Comment: "I like https://www.google.com/" becomes "I like more info".
If that's the case, perhaps adding a filter to functions.php to search for and replace URL might do the trick:
// define the get_comment_text callback
function filter_get_comment_text( $comment_comment_content, $comment, $args ) {
// Regular expression to find URL
$pattern = '/(https?):\/\/(www\.)?[a-z0-9\.:].*?(?=\s)/i';
// Replace url with linked "more info"
$replacement = 'more info';
// Find matches & replace
$newcomment = preg_replace($pattern, $replacement, $comment_comment_content);
// Return the comment
return $newcomment;
};
// add the filter
add_filter( 'get_comment_text', 'filter_get_comment_text', 10, 3 );

How to show record made by tinyMCE?

I use tinyMCE to input the blog content record. i write this:
This is my new entry
i format it with justify and heading 1
then i display it, but the result is:
<h1 style="text-align: center;">This is my new entry</h1>
how to show it correctly
Tinymce adds html tags to all the text that you enter in it. By default it adds <p> to all the text that you enter and save.You need to use the function strip_tags() in PHP to remove the html tags.
Example:
$a = "<h1 style="text-align: center;">This is my new entry</h1>"; //this data is have assigned for demo you can get it from any place
echo strip_tags($a);
$text = 'Test paragraph. Other text';
echo strip_tags($text);
echo "\n";
// Allow and
echo strip_tags($text, '');
//output :
//Test paragraph. Other text
//Test paragraph. Other text

How can I make a script, that would paste code after each line?

I'm creating a web, where I want one part of it to look something like this: Some text, followed by an audio tag with path to the audio file and then an underline(not an underline to text, but line that would separate the content) below that. This then repeats many times.
What I would like to do is to just write the line of text and then maybe write a tag or just leave the line of text. The script would just go thruough all the lines and paste there the audio tag with the filename (which would be just name_of_file*, where * would get by one bigger on each line) and the underline.
What I'm doing now is just pasting the audio tag and an hr tag after each line of text, then going through and manually writing the numbers of the files. And that is just stupid and tidious.
I'm familiar with HTML, CSS, and PHP. But I guess there is probably easier solution to this than to use PHP.
I hope you can understand what I mean and thanks for every answer!
You could get a text editor to do this, but if you already know PHP, I think it's much simpler to use that.
$array=array(
'line of text',
'another line of text',
array('text'=>'This one is special cause it has a tag', 'tag'=>'myTag'),
'yet another line o text',
.....
);
foreach($array as $index=>$val){
if($index>0){
echo '<hr>';
}
if(is_array($val)){
echo $val['text'];
echo 'tag: '.$val['tag'];
}else{
echo $val;
}
echo '<audio src="whatever_'.$index.'.oog">..put some <source> here...</audio>';
}
Try something like this.
The audio files and description text is saved to an array.
PHP does a for() loop through the array and outputs the text, the link and a < hr > which is a horizontal ruler (or a underline as you said).
Quick example:
$audiofiles = array(
'Some text for the audio file 1' => 'path/to/file.mp4',
'Some text for the audio file 2' => 'path/to/filetwo.mp3'
);
$n = count($audiofiles);
for($i=0; $i<$n; $i++)
{
echo $audiofiles[$i][0];
echo 'CLICK FOR LINK';
echo '<hr>';
}

How can I insert text in an html element stored in a variable?

I have a program which is copying the text from another website and showing it.
It is storing the text in a variable $string.
The variable is containing html tags in it and I want to add text before a html tag stored in the variable.
For example: $string="<div id='1'><div id='game'></div>"; I want to add text before the div whose id is game.
To add the text before the div whose id is 'game'. simply use:
$string = "<div id='1'><div id='game'></div>";
$new = "texttoinsert";
$pos = "<div id='game'></div>";
echo str_replace($pos, $new.$pos ,$string);
In php the easiest way to do this would be using str_replace (http://www.php.net/manual/en/function.str-replace.php).
$textToInsert = "test";
$string = str_replace("<div id='game'>", $textToInsert."<div id='game'>" ,$string);
For that particular case the following works:
$($string).prepend("text");
DEMO
Using jQuery the solution is simple:
var text = $("<div id='1'><div id='game'></div></div>");
$('#1', text).prepend('text-to-insert');
and the result HTML can be obtained like this: text.html()
I hope this help.

PHP preg_replace: Need to modify hyperlink text if hyperlink contains 'youtube.com'

I'm almost there!
Here is the string I am trying to adjust, and my preg_replace attempt.
$description_string = '<p>Here is the test link: “Man or Muppet” with other text afterwards.</p>';
$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<\/a>)+/', '$0Watch This Video$2', $description );
The result I'm getting is incorrect:
Here is the test link: “Man or Muppet”Watch This Video with other text afterwards.
Any help would be greatly appreciated! THanks!
Well, not sure if you are trying to get the actual title in there. But here is what I came up with:
<?php
$description_string = '<p>Here is the test link: \'Man or Muppet\' with other text afterwards.</p>';
$description = preg_replace( '/(<a[^>]+youtube[^>]*>)+[^"]*(<\/a>)+/', '$1Watch This Video$2', $description_string );
echo $description;
?>
Result:
<p>Here is the test link: Watch This Video with other text afterwards.</p>
Your biggest issue was with the quotes (") being in the title. It is cutting off the anchor tags. While using $0 is also incorrect. You'll need to use $1.
This may not be exactly what you need, but its a quick monkey patch for you.

Categories