finding URL in a text plain and change it to hyperlink [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Find url from string with php
I found this code:
but 2 link did not make properly.
save my code in a php file for testing.
$a = <<<_END
www.coders.com/coder6602.html => Work
www.avan.ir?a=21 => Work
www.limited.ndot.in/kansas/#?w=500 => Not Work
http://www.sales.com => Work
http://www.mediafire.com/?298nlpla3eys9g6 => Work
http://diary.com/files/draft.html و http://www.logo.net/plogo.swf => Not Work (this is two link)
_END;
/*
limited.ndot.in/kansas/#?w=500
diary.com/files/draft.html و logo.net/plogo.swf
*/
function strHyperlink($str){
$pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?#)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"<>|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
$str = preg_replace('/http:\/\//','www.', $str);
$str = preg_replace('/www.www./','www.', $str);
$str = preg_replace($pattern_url,"\\0", $str);
return preg_replace('/www./','',$str);
}
echo strHyperlink($a);

Use this regex: \b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
You can test it on: http://gskinner.com/RegExr/
I have just tested this regex with your url's and works perfectly.

Related

Rewriting HTML anchor using str_replace and regex [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
The following is automatically output via editor:
1001-web-file
I would like to add an ID:
<a id="replace" href="https://someurl.com/1001-web-file.pdf">1001-web-file</a>
Then use str_replace and a regular expression to find the anchors in question and replace with:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
What I've managed to do is:
$replace = array(
'<a id="pdfthumb" href="' => '<img src="',
'.pdf">pdfthumb</a>' => '-pdf-290x300.jpg"/></br>'
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
This works to tear down the anchor tag and rebuild as an img. But I can't do much more. I played around with some regex to create a wildcard and realized I need to create a variable for the href to use when I rebuild the HTML, but I'm stuck.
Any insight is much appreciated :)
In your case, I think it should be easier if you do it on client side using javascript.
My background is not PHP but you can use the same pattern to test with your PHP code:
Input:
1001-web-file
Output:
<img src="https://someurl.com/1001-web-file-pdf-290x300.jpg"/>
var input = '1001-web-file';
var pattern = /href=\"(.+)\.pdf\"/;
var match = input.match(pattern)[1];
input = input.replace(/(<a.*>).*(<\/a>)/, '$1<img src="' + match + '-pdf-290x300.jpg">$2');
console.log(input)
Here is translated php code of Tan javascript answer
$input = '1001-web-file';
preg_match('/href="(.+)\.pdf"/', $input, $m);
$match = $m[1];
$input = preg_replace('/(<a.*>).*(<\/a>)/', '$1<img src="'. $match .'-pdf-290x300.jpg">$2', $input);
echo $input;

How to replace all matching regex of a sting in PHP [duplicate]

This question already has answers here:
Remove all html tags from php string
(9 answers)
Regex str_replace
(2 answers)
Closed 5 years ago.
sorry for not beeing able to put the title exactly what my question, I can't find a way to ask it.
So im trying to replace all ' in a text inside and replace it for ' in php
I have a variable $desc = "he's such a good person and he'll be very successfull";
and I'm trying to do the following
$desc = str_replace("'","'",$desc);
But no success is there a way to use regex in str_replace?
Yes it looks to be fine now... for some reason.
Is there a way to use regex for it?
for example to remove html tags from the text?
$desc = "<strong> he's such a good person </strong> <br /> he'll be very successfull";
$desc = str_replace("<*>"," ",$desc);
You may try to use the correct PHP function to do this job, so please take a look at : preg_replace doc.
In your case, you would like to use like this :
preg_replace('/'/', "'", $desc);
Take a look at this execution:
https://eval.in/800948
Does it have to be a regex because this is a lot simpler way to do it
$desc = "he's such a good person and he'll be very successfull";
$new = str_replace(''', "'", $desc);
echo $new;
RESULT:
he's such a good person and he'll be very successfull
You don't need to use regex as it will be more complex
use
$desc = "he's such a good person and he'll be very successfull";
$desc = str_replace("'","'",$desc);
echo "after replace :"." ".$desc;
it is more simpler :)

Convert emoji into custom images [duplicate]

This question already has an answer here:
Convert keyboard emoticons into custom png and vice versa
(1 answer)
Closed 7 years ago.
Input - hey I'm smiling 😀
Output - hey I'm smiling <span class ="smile"></span>
Code
$emoticons = array('😀' =>'<span class ="smile"></span>') ;
$str = strtr($str, $emoticons) ;
I can't use str_replace because I have more than one element in $emoticons array.
This above code is not working the input and output remains same.
This works for me:
<?php
$str = "hey I'm smiling 😀 and I'm crying 😢 😢"; // input
$emoticons = array('😀' =>'<span class="smile"></span>','😢' =>'<span class="cry"></span>') ; // array of emoticons and spans
$output = strtr($str, $emoticons); // change emoticons from array to spans from array
echo $output; // print it
?>

PHP replace hyphen with space [duplicate]

This question already has answers here:
how to replace hyphen with blank space / white space? php
(2 answers)
Closed 8 years ago.
So, I have a piece of code I am using in most of my pages which sets the page title as the file name. This is because I don't want to have to change the title each time I create a new page.
This all works fine, except I don't want to have spaces (or rather, %20) being shown in the url when the file name contains spaces. I'd much rather have hyphens in place of the spaces, as it looks cleaner to the user. However, this means PHP will set the page title also with hyphens instead of spaces, which, frankly, looks ugly.
Is there a way I can rewrite this code to replace the hyphens with spaces?
The code:
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst($path_parts['filename']);
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use str_replace to replace the - with a space.
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst(str_replace('-', ' ', $path_parts['filename']));
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use implode adn explode for this
$string = "the string";
$arr = explode("string to replace",$string);
$string = implode("string to add ",$arr);
echo $string;

regex - prevent two characters from being adjacent [duplicate]

This question already has answers here:
Batch script to replace PHP short open tags with <?php
(13 answers)
Closed 9 years ago.
I need to replace all PHP lines that are like <?=[something]?> with this: <?php echo [something]; ?> The problem is that there can be almost anything in that something clause even ? and < but they can't be adjacent. I am kinda new to regexes and wrote this very messy expression: \<\?\=([a-zA-Z0-9()=#<>\[\]\\/'"._$\?:, \-]*)([;]*)\?\> and replaced it with <?php echo \1; ?>.
It works but it can't match something like this:
<?=[something]?><tr><td><?=[something]?> when it's all in one line. It matches the whole line altogether.
Any help will be greatly appreciated.
This code should work:
$s = '<?=[something]?><tr><td><?=[something]?>';
$s = preg_replace('/(<\?)=\s*(\[[^]]*\])\s*(\?>)/', '$1php echo $2; $3', $s);
var_dump($s);
OUTPUT:
string(56) "<?php echo [something]; ?><tr><td><?php echo [something]; ?>"

Categories