Simplest way to convert all html links in a string using PHP - php

I am trying to convert a block of text that contains html text - i'd like to find all http links and convert them for link tracking purposes.
So eg anything like this in a string would be converted to the latter
Some Link
Some Link
Can anyone how to do this taking into account the original string will consists of all sorts of html, images etc..

Use this regex : (UPDATED)
<?php
$str = '<h1>Page Title</h1>Google';
$text = preg_replace("/href=\"http\:\/\/([a-zA-Z0-9\-]+\.[a-zA-Z0-9]+\.[a-zA-Z]{2,3}(\/*)?)/","href=\"http://www.mysite.com/tracking.php?url=$1\"",$str);
echo $text;
?>
Outputs :
<h1>Page Title</h1>Google

$str = '<h1>Page Title</h1>Google';
$text = preg_replace('href=\"http\://([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?)\"', 'href=\"YOUR_TRACKING_URL=$1\"', $str);
echo $text;
Warning: preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in /home/....
(sorry for the duplication)

Related

php - Replace Template Code with HTML

I got a Button, that creates this template code
[5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)
The filename and file size vary und the user can change the ##LINKNAME##, as well.
This code goes to a database and when I get it back, I want to replace it to
##LINKNAME## <i>(96.51 kb)</i>
I think I need to use preg_replace() but I am not really good at regular expressions.
I stopped here:
<?php
$string = ' [5afc4076e9f1b1526481014.pdf]##LINKNAME## (96.51 kb)';
$regex = '[[a-zA-Z0-9]+.pdf](.*?)\s';
$replace = 'I DONT KNOW';
echo preg_replace($regex, $replace, $string);
?>
I know that this is a complete mess, but I'm not getting any results as long as I don't know the regex and the correct $replace.
Regex: ^\[([^\]]+)\](\w+)\s\(([^)]+)\)$
Replace with: \2<i>(\3)</i>
Demo

htmlspecialchars and make links clickable

I have a PHP script which processes user input. I need to escape all special characters, but also make links clickable (turn them into <a> elements). What I need is:
function specialCharsAndLinks($text) {
// magic goes here
}
$inp = "http://web.page/index.php?a1=hi&a2=hello\n<script src=\"http://bad-website.com/exploit.js\"></script>";
$out = specialCharsAndLinks($inp);
echo $out;
The output should be (in HTML):
http://web.page/index.php?a1=hi&a2=hello
<script src="http://bad-website.com/exploit.js"></script>
Note that the amperstand in the link stays in the href attribute, but is converted to & in the actual content of the link.
When viewed in a browser:
http://web.page/index.php?a1=hi&a2=hello
<script src="http://bad-website.com/exploit.js"></script>
I eventually solved it with:
function process_text($text) {
$text = htmlspecialchars($text);
$url_regex = "/(?:http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+(?:\/\S*)?/";
$text = preg_replace_callback($url_regex, function($matches){
return ''.$matches[0]."";
}, $text);
return $text;
}
The first line html-encodes the input.
The second line defines the URL regex. Could be improved, but working for now.
The 3rd line uses preg_replace_callback, a function which is like preg_replace, but instead of supplying it with a replacement string, you supply a replacement function that returns the replacement string.
The 4th line is the actual function. It's quite self-documenting. htmlspecialchars_decode undoes the actions of htmlspecialchars (therefore making the link valid if it contained an amperstand).
Try this:
$urlEscaped = htmlspecialchars("http://web.page/index.php?a1=hi&a2=hello");
$aTag = 'Hello';
echo $aTag;
Your example doesn't work because if escaping whole html tag, a tag will never get processed by the browser, instead it will just display as plain text.
As you can see, stackoverflow escapes our whole input (questions/answers ...), so we can actually see the code, and not letting browser to process it.

How to search and delete string in php

I am attempting to use an RSS feed from Twitter (the ones that will be gone a year from now) with Feedburner but a line of code in the xml is causing a parsing error in feedburner. I think it's
xmlns:twitter="http://api.twitter.com"
I am looking for a way to give this information to feedburner in a way that it likes: namely php including the contents of the RSS xml page in an xml on my own site (with the offending string removed). Including the xml contents is a piece of cake - all I need is a piece of code to say iff string fragment == xmlns:twitter="http://api.twitter.com" then string frament = "".
Anybody any idea of the syntax for this?
You can try with str_replace:
str_replace('xmlns:twitter="http://api.twitter.com"', '', $string);
Also, you can try to urlencode http://api.twitter.com.
Alternatively, you could use preg_replace function:
http://php.net/manual/en/function.preg-replace.php
<?php
$string = 'string to be searched';
$pattern = 'a particular pattern';
$replacement = ''; //deletion
echo preg_replace($pattern, $replacement, $string);
?>

How can i read a file in php only to a certain character

the fseak function sets a cursor to the file but only an offset, a number. Is there a function or a way i can set the cursor to a text file after reading a character or a string? Example, lets say i have this text file:
Hi, welcome to your page!
Is there a way i can insert my name for instance, right after the word "Hi" in the text file?
You can read the file into a string, manipulate the string, then re-save the string to the file. If you're running PHP5 you should be able to do this:
$str = file_get_contents('filename.txt');
$str = str_replace('Hi', 'Hi Joe', $str);
file_put_contents($str);
Just put the file contents into a string variable and use PHPs string functions, like strpos(), substr(), etc. When you're done, write back to the file.
you can use substr_replace
I'm inserting my string at index 2 and replacing 0 characters.
//$data = file_get_contents("file.txt");
$data = "Hi, welcome to your page!";
echo substr_replace($data, " Ilia", 2, 0);
this way you don't have to do any string based searches

Capture newline from a textarea input

I have a textarea form in my html. If the user hits enter between 2 sentences that data should be carried over to my PHP.
Currently if the user enters:
Apple
Google
MS
and my PHP code is:
$str = $_POST["field"];
echo $str;
I get
Apple Google MS
as the output. I want output to be like this
Apple
Google
MS
what should I do?
Try nl2br() instead:
echo nl2br($str);
The newlines should be included in the string that you get from $_POST["field"]. However, if you then use that string as output in HTML, newlines will be treated as whitespace. To force the line breaks, use preg_replace("/\n/", "<br />", $str).
This is because when you echo it it's being displayed as HTML. A \n character is interpreted as a space. If you view the source you'll see your desired output.
To convert \n to <br> use:
echo nl2br( $str );

Categories