React Native, how to turn a comment into a URL? - url

I was wondering how I can turn a comment (such as stackoverflow.com) to a post as a URL so that when clicked, it will go straight to the website?
Thanks for your help in advance

You can pre-process your text and look for things that might be a URL. Look at this answer here: Regex to match URL.
You'd want to take your text, split it by white-space, then for each white-space-separated word, check if it's a URL. If it's a URL, then output a proper <a> tag surrounding it. If not, just output the word.

Related

How can I change URL symbols?

I want to use Georgian characters in my URL (e.g "https://www.example.ge/post/ქართული-სიმბოლოები") and my result in a browser is correct but in SEO audit look as "https://www.example.ge/post/ááá¢ááááááªáá
Does anyone know how I can fix this?
Try using URL encoding. There are quite a few tools on the web. E.g., on one of these tools your url is encoded like this:
https%3A%2F%2Fwww.example.ge%2Fpost%2F%E1%83%A5%E1%83%90%E1%83%A0%E1%83%97%E1%83%A3%E1%83%9A%E1%83%98-%E1%83%A1%E1%83%98%E1%83%9B%E1%83%91%E1%83%9D%E1%83%9A%E1%83%9D%E1%83%94%E1%83%91%E1%83%98
In the address bar this will be shown as your Georgian characters.

Missing the middle or a URL

I was wondering if there was a way to find a URL when I am missing the middle. For example, I know that the beginning will be https://welldressedwolf/products/
and the end will be pretty-things but I do not know the middle portion.
With javascript you can get the full URL using window.location.href
After that you can remove the parts you dont want and you will have the middle.
More info: https://www.w3schools.com/js/js_window_location.asp
If you're looking to manually look up the address of a page, you could do so with a google search that specifies the site.
Try a site specific google search using something like the following:
"pretty things" site:welldressedwolf.com

Detect and replace URLs in text

I want to detect and replace URLs in texts input by users. An example worth thousand words:
Here's a link to stackoverflow.com, so is http://stackoverflow.com.
=>
Here's a link to [stackoverflow.com](http://stackoverflow.com), so is [http://stackoverflow.com](http://stackoverflow.com).
All I found from Google is how to detect URLs and change them to <a> tags. Is there a way that I can detect URLs, and replace them with custom code blocks to generate something as the example above? Thanks a lot!
The tricky part of this is finding a regexp which will match all urls. eg this might work, from http://ryanangilly.com/post/8654404046/grubers-improved-regex-for-matching-urls-written
regexp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/?)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s\`!()\[\]{};:\'\".,<>?«»“”‘’]))/i
Once you've got your regexp, then use gsub with a block, eg
text = "Here's a link to stackoverflow.com, so is http://stackoverflow.com."
=> "Here's a link to stackoverflow.com, so is http://stackoverflow.com."
text.gsub(regexp){|url| "FOO#{url}BAR"}
=> "Here's a link to stackoverflow.com, so is FOOhttp://stackoverflow.comBAR."
Note that this doesn't do anything with the first one in the text (that doesn't have the protocol), because it's not a url. if you were expecting it to pick up the first one too then that's going to be much harder for you.

Removing HTML url tags in iOS

I am writing an iOS app that downloads some data from a server that's not under my control. I am not using custom data detectors. The strings in the returned JSON still contain their HTML url tags, and I want to remove them because I want to display the strings in a UITextView, and these kind of strings
<strong>Instagram</strong> / <strong>Behance</strong>
<strong>Live Now</strong>
What I really want is this:
Instagram Behance
Live Now
What is the best way to go about this?
Should I strip the url tags from the text using regex?
Would I lose the link "descriptions" (in the above example, "Instagram" and "Behance") when I do that?
Would this be way easier using a UIWebView?
If this would be too hard/impossible, it'd be okay to only have the urls, without their descriptions.
Thank you!
Should I strip the url tags from the text using regex?
No. HTML is too complex to be properly parsed using a RegEx. You'll need an XML parser.
Would I lose the link "descriptions" (in the above example, "Instagram" and "Behance") when I do that?
You wouldn't have to using an XML parser. Using a RegEx, you might, especially if you can't control exactly what's returned.
Would this be way easier using a UIWebView?
Yep. That's what I would do, unless you have a good reason not to.

dynamic seo title for news articles

I have a news section where the pages resolve to urls like
newsArticle.php?id=210
What I would like to do is use the title from the database to create seo friendly titles like
newsArticle/joe-goes-to-town
Any ideas how I can achieve this?
Thanks,
R.
I suggest you actually include the ID in the URL, before the title part, and ignore the title itself when routing. So your URL might become
/news/210/joe-goes-to-town
That's exactly what Stack Overflow does, and it works well. It means that the title can change without links breaking.
Obviously the exact details will depend on what platform you're using - you haven't specified - but the basic steps will be:
When generating a link, take the article title and convert it into something URL-friendly; you probably want to remove all punctuation, and you should consider accented characters etc. Bear in mind that the title won't need to be unique, because you've got the ID as well
When handling a request to anything starting with /news, take the next part of the path, parse it as an integer and load the appropriate article.
Assuming you are using PHP and can alter your source code (this is quite mandatory to get the article's title), I'd do the following:
First, you'll need to have a function (or maybe a method in an object-oriented architecture) to generate the URLs for you in your code. You'd supply the function with the article object or the article ID and it returns the friendly URL with the ID and the friendly title.
Basically function url(Article $article) => URL.
You will also need some URL rewriting rules to remove the PHP script from the URL. For Apache, refer to the mod_rewrite documentation for details (RewriteEngine, RewriteRule, RewriteCond).

Resources