How to preserve single and double quotes in a link using REGEX? - php

I have a regex code which finds all URLs and replaces them with a HTML link. Here is my code:
// initializing
$str = "this is a good website www.example.com/classname/methodname/arg";
$rexProtocol = '(https?://)?';
$rexDomain = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=#_\':;!a-zA-Z\x7f-\xff]+?)?';
function callback($match){
// Prepend http:// if no protocol specified
$completeUrl = $match[1] ? $match[0] : "http://{$match[0]}";
$DetectProperName = strlen($match[2].$match[3].$match[4]) > 20 ? "...".substr($match[2].$match[3].$match[4],0,20) : $match[2].$match[3].$match[4];
return ''.$DetectProperName. '';
}
echo $str = preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&",'callback', htmlspecialchars($str));
Also here is the output:
this is a good website ...www.example.com/clas
Also here is a fiddle
Well, that's ok and it works as well for links. Now my question is about when input is containing a quote ' or ". That regex will add a \ next to it. How can I fix it? I want such a regex be not sensitive to quotes.
Here is an example:
Input:
$str = 'this is a " (quote)';
Current Output:
this is a \" (quote)
What I want:
this is a " (quote)
How can I do that?
Edit: According to some tests, I figured out that change single/double quotes to ASKII code. How can I prevent it?

Related

Text between less than and greater than not output PHP

i use (str_replace) function to replace ##ID## in youtube url with this regular expression : (?P<id>[a-z-A-Z_0-9]+)
so i use this code to do this :
<?php
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo $lastchange;
?>
i get the output in the browser like this : https://www.youtube.com/watch?v=(?P[a-z-A-Z_0-9]+), its looks like <id> not show up !
i try this simple code :
<?php
echo "This is my <id>";
?>
but i just get this is my in the browser !
What's the probleme ? and how i can fix it , thanks
is being interpreted as HTML so your browser is parsing it and since it is not a renderable element, it shows nothing. Try:
<?php
echo "This is my <id>
?>
As for the str_replace, it's doing exactly what the function is supposed to be doing. If you're looking to use regular expressions in string replacements, use preg_replace
The tag <id> is being removed by your browser. It is really there if you watch the source code. Maybe you should try:
$urlbase = 'https://www.youtube.com/watch?v=##ID##';
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
echo urlencode( $lastchange );
Problem is with the line:
$lastchange = str_replace('##ID##', '(<id>[a-z-A-Z_0-9]+)', $urlbase);
str_replace does not use regex.
You will need preg_replace
$pattern = '(<id>[a-z-A-Z_0-9]+)'
$replacement = '##ID##'
$string = $urlbase
$lastchange = preg_replace($pattern, $replacement, $string);
Also < and > are html entities which means they are reserved chars for HTML they have some special meanings if you want to show them then you must use there entity name eg < and > in your case respectively.
<?php
echo " echo "This is my <id>";
?>

Replace url strings in PHP

I have a string for example : I am a boy
I want to show this on my url for example in this way : index.php?string=I-am-a-boy
My program :
$title = "I am a boy";
$number_wrds = str_word_count($title);
if($number_wrds > 1){
$url = str_replace(' ','-',$title);
}else{
$url = $title;
}
What if I have a string : Destination - Silicon Valley
If I implement the same logic my url will be : index.php?string=Destination---Silicon-Valley
But I want to show only 1 hyphen.
I want to show a hyphen instead of a plus sign..
url_encode() will eventually insert plus symbols.. So it's not helping here.
Now if I use minus symbol then if the actual string is Destination - Silicon Valley, then the url will look like
Destination-Silicon-Valley and not
Destination---Silicon-Valley
Check this stackoverflow question title and the url. You will know what I am saying.
Check this
Use urlencode() to send strings along with an url:
$url = 'http://your.server.com/?string=' . urlencode($string);
In comments you told, that you don't want urlencode, you'll just replace spaces by - characters.
First, you should "just do it", the if conditional and str_word_count() is just overhead. Basically your example should look like this:
$title = "I am a boy";
$url = str_replace(' ','-', $title);
That's it.
Further you told that this would make problems if the original string already contains a -. I would use preg_replace() instead of str_replace() to solve that problem. Like this:
$string = 'Destination - Silicon Valley';
// replace spaces by hyphen and
// group multiple hyphens into a single one
$string = preg_replace('/[ -]+/', '-', $string);
echo $string; // Destination-Silicon-Valley
Use preg_replace instead:
$url = preg_replace('/\s+/', '-', $title);
\s+ means "any whitespace character (\t\r\n\f (space, tab, line feed, newline)).
use urlencode:
<?php
$s = "i am a boy";
echo urlencode($s);
$s = "Destination - Silicon Valley";
echo urlencode($s);
?>
return:
i+am+a+boy
Destination+-+Silicon+Valley
and urldecode:
<?php
$s = "i+am+a+boy";
echo urldecode($s)."\n";
$s = "Destination+-+Silicon Valley";
echo urldecode($s);
?>
return:
i am a boy
Destination - Silicon Valley
just use urlencode() and urldecode(). It’s for sending Data with GET in the URL.

How to ignore single quotes in regex using preg_replace function in PHP?

I am basically trying to transform any hash-tagged word in a string into a link:
Here is what my code looks like:
public function linkify($text)
{
// ... generating $url
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/$1>#$1</a>", $text);
return $text;
}
It works pretty good excepting the case when that $text contains a single quote. Here are
Example1:
"What is your #name ?"
Result: "What is your #name?" Works fine.
Example2:
"What's your #name ?"
Result: "What's your #name?" Does not work, I want
this result: "What's your #name?"
Any idea about how I can get rid of that single quote problem using PHP ?
EDIT1:
Just for info, before or after html_entity_decode($text) I got
"What's your #name?"
Something like this.
$string = "' \'' '";
$string = preg_replace("#[\\\\']#", "\'", $string);
Something is protecting your html entities. This can save your life if the string is coming from a get/post request - but iI it's from a trusted source just use html_entity_decode to convert it back. This 39-thing is a way to express the single quote as you might have realized.
if the problem is html_entities, then maybe you only need to html_entity_decode your $text
$text = preg_replace("/\B#(\w+)/", "<a href=" . $url . "/html_entity_decode($1)>#$1</a>", $text);
Thanks all for your suggestions, I've finally sorted this out with this :
html_entity_decode($str, ENT_QUOTES);

Merging preg_match_all and preg_replace

I have some code running which finds out hashtags in the string and turns them into links. I have done this using preg_match_all as shown below:
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$long = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
Also, for my search script, I need to bold the searched keywords in the result string. Something similar to the below code using preg_replace:
$string = "This is description for Search Demo";
$searchingFor = "/" . $searchQuery . "/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $string);
The problem that I am having is that both have to work together and should be thrown as a combined result. One way I can think of is to run the resultant string from preg_match_all with the preg_replace code but what if the tags and the searched string are the same? The second block will bold my tag as well which is not desired.
update the code i'm running based on the answer given below but it still doesn't work
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$postLong = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
And immediately after this, i run this
$searchingFor = "/\b.?(?<!#)" . $keystring . "\b/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $postLong);
Just so you know, this is all going inside a while loop, which is generating the list
You just need to modify you the search pattern to avoid ones that start with a '#'
$postLong = "This is description for Search Demo";
if(preg_match_all('/(#[A-z_]\w+)/', $postLong, $arrHashTags) > 0){
foreach ($arrHashTags[1] as $strHashTag) {
$postLong = str_replace($strHashTag, ''.$strHashTag.'', $postLong);
}
}
# This expression finds any text with 0 or 1 characters in front of it
# and then does a negative look-behind to make sure that the character isn't a #
searchingFor = "/\b.?(?<!#)" . $searchQuery . "\b/i";
$replacePattern = "<b>$0<\/b>";
preg_replace($searchingFor, $replacePattern, $postLong);
Or if you don't need an array of the available hashes for another reason, you could use preg_replace only.
$postLong = "This is description for #Search Demo";
$patterns = array('/(#[A-z_]\w+)/', "/\b.?(?<!#)" . $searchQuery . "\b/i");
$replacements = array(''.$0.'', ' "<b>$0<\/b>');
preg_replace($patterns, $replacements, $postLong);

PHP Regex to convert text before colon to link

I need to find the first occurance of a colon ':' and take the complete string before that and append it to a link.
e.g.
username: #twitter nice site! RT www.google.com : visited!
needs to be converted to:
username: nice site! RT www.google.com : visited!
I've already got the following regex that converts the string #twitter to a clickable URL:
E.g.
$description = preg_replace("/#(\w+)/", "#\\1", $description);
Any ideas : )
I'd use string manipulation for this, rather than regex, using strstr, substr and strlen:
$username = strstr($description, ':', true);
$description = '' . $username . ''
. substr($description, strlen($username));
$regEx = "/^([^:\s]*)(.*?:)/";
$replacement = "\1\2";
I have not tested the code, but it should work as is. Basically you need to capture after #twitter too.
$description = preg_replace("%([^:]+): #twitter (.+)%i",
"#\\1: \\2",
$description);
The following should work -
$description = preg_replace("/^(.+?):\s#twitter\s(.+?)$/", "#\\1: \\2", $description);
Direct answer to your question:
$string = preg_replace('/^(.*?):/', '$1:', $string);
But I assume that you are parsing twitter RSS or something similar. So you can just use /^(\w+)/.

Categories