I have a coupon site that display store urls on my store pages. What I want is for only .com at end of each store without showing http:// variations in the beginning
here is my code that displays a store url and I just want domain.com to be displayed instead of http://www.domain.com, also may show as http://domain.com
<p class="store-url"><a href="<?php echo $url_out; ?>" target="_blank"><?php echo $stores_url; ?>
It displays like this because of this function
<div class="store">
<?php // grab the store meta data
$term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
$stores_url = esc_url(get_metadata(APP_TAX_STORE, $term->term_id, 'clpr_store_url', true));
$dest_url = esc_url(get_metadata(APP_TAX_STORE, $term->term_id, 'clpr_store_aff_url', true));
// if there's a store aff link, then cloak it. else use store url
if ($dest_url)
$url_out = esc_url(home_url(CLPR_STORE_REDIRECT_BASE_URL . $term->slug));
else
$url_out = $stores_url;
?>
What can be done................
Quick and dirty - to demonstrate the possible functions...
<?php
function cleanInputString($inputString) {
// lower chars
$inputString = strtolower($inputString);
// remove whitespaces
$inputString = str_replace(' ', '', $inputString);
// check for .com at the end or add otherwise
if(substr($inputString, -4) == '.com') {
return $inputString;
} else {
return $inputString .'.com';
}
}
// example
$inputStrings = array(
'xyzexamp.com',
'xyzexamp',
'xyz examp'
);
foreach($inputStrings as $string) {
echo('input: '. $string .'; output: '. cleanInputString($string) .'<br />');
}
?>
OUTPUT:
input: xyzexamp.com; output: xyzexamp.com
input: xyzexamp; output: xyzexamp.com
input: xyz examp; output: xyzexamp.com
The "right way" is probably to use the PHP URL-processing:
Break the URL up using http://php.net/manual/en/function.parse-url.php
Remove the scheme element of the resulting array using unset
Build it again using http://www.php.net/manual/en/function.http-build-url.php
This is what preg_replace was made for:
<?php
$http_url = 'http://www.somestore.com/some/path/to/a/page.aspx';
$domain = preg_replace('#^https?://(?:www\.)?(.*?)(?:/.*)$#', '$1', $http_url);
print $domain;
?>
This code will print out
somestore.com
Related
i'm trying to do something in PHP
I'm trying to get the link of an image -> store it to my DB, but I'd like the user to be able to store text before it, and after it, I've gotten my hands on a similar function for links, but the image part is missing.
As you can see the turnUrlIntoHyperlink does a regex check over the entire arg passed, turning the text that contains it to the url, so users can post something like
Hey check this cool site "https://stackoverflow.com" its dope!
And the entire argument posting to my database.
However i can't seem to get the same function working for the Convert Image, as it simply won't post and removed text before/after it before when i made the attempt.
How would i do this in a correct way, and can i combine these 2 functions in to 1 function?
function convertImg($string) {
return preg_replace('/((https?):\/\/(\S*)\.(jpg|gif|png)(\?(\S*))?(?=\s|$|\pP))/i', '<img src="$1" />', $string);
}
function turnUrlIntoHyperlink($string){
//The Regular Expression filter
$reg_exUrl = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $string, $url)) {
// Loop through all matches
foreach($url[0] as $newLinks){
if(strstr( $newLinks, ":" ) === false){
$link = 'http://'.$newLinks;
}else{
$link = $newLinks;
}
// Create Search and Replace strings
$search = $newLinks;
$replace = ''.$link.'';
$string = str_replace($search, $replace, $string);
}
}
//Return result
return $string;
}
more explained in detail :
When i post a link like https://google.com/ I'd like it to be a href,
But if i post an image like https://image.shutterstock.com/image-photo/duck-on-white-background-260nw-1037486431.jpg , i'd like it to be a img src,
Currently, i'm storing it in my db and echoing it to a little debug panel,
Do you mean that you want to make an <img> inside <a> element?
Your turnUrlIntoHyperlink function have captured the url successfully, so we can just use explode to get string before and after the link.
$exploded = explode($link, $string);
$string_before = $exploded[0];
$string_after = $exploded[1];
Code example:
<?php
function turnUrlIntoHyperlink($string){
//The Regular Expression filter
$reg_exUrl = "/(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))/";
// Check if there is a url in the text
if(preg_match_all($reg_exUrl, $string, $url)) {
// add http protocol if the url does not already contain it
$newLinks = $url[0][0];
if(strstr( $newLinks, ":" ) === false){
$link = 'http://'.$newLinks;
}else{
$link = $newLinks;
}
$exploded = explode($link, $string);
$string_before = $exploded[0];
$string_after = $exploded[1];
return $string_before.'<img src="'.$link.'">'.$string_after;
}
return $string;
}
echo turnUrlIntoHyperlink('Hey check this cool site https://stackoverflow.com/img/myimage.png its dope!');
Output:
Hey check this cool site <img src="https://stackoverflow.com/img/myimage.png"> its dope!
Edit: the question has been edited
Since an image URL is just another kind of link/URL, your logic should go like this pseudocode:
if link is image and link is url
print <img src=link> tag
else if link is url and link is not image
print <a href=link> tag
else
print link
So you can just write a new function to "merge" those two function:
function convertToImgOrHyperlink($string) {
$result = convertImg($string);
if($result != $string) return $result;
$result = turnUrlIntoHyperlink($string);
if($result != $string) return $result;
return $string;
}
echo convertToImgOrHyperlink('Hey check this cool site https://stackoverflow.com/img/myimage.png its dope!');
echo "\r\n\r\n";
echo convertToImgOrHyperlink('Hey check this cool site https://stackoverflow.com/ its dope!');
echo "\r\n\r\n";
Output:
Hey check this cool site <img src="https://stackoverflow.com/img/myimage.png" /> its dope!
Hey check this cool site https://stackoverflow.com/ its dope!
The basic idea is that since image url is also a link, such check must be done first. Then if it's effective (input and return is different), then do <img> convertion. Otherwise do <a> convertion.
I am trying to compare the current URL with the URLs retrieved from the database but for some reason only works if it matches the first array.
$url = get_permalink();
function check_links($url){
$db_links= get_option('db_links');
$exclude = explode(',',$db_links);
if(in_array($url, $exclude)){
echo "display:none";
}
}
After I dump $exclude, I get
array(3){
[0]=>string(16) "https://test.com"
[1]=>string(18) "https://test.com/2"
[2]=>string(19) "https://test.com/22"
[3]=>string(20) "https://test.com/235"
}
I want to compare my current URL with the list above. If it shows I want to echo "display:none";
My current URL is https://test.com/22
if(in_array($url, $exclude)){
echo "display:none";
}
I found out that it works only if my URL is https://test.com, for other cases it doesn't work.
In this case your string have some \n and space than not showed and which cause errors, for handeling this errors you most replace the \n\ and space with nothing, folow mycode:
$url = get_permalink();
function check_links($url){
$db_links= get_option('db_links');
$exclude = array_map('trim', explode(',',$db_links));
if(in_array($url, $exclude)){
echo "display:none";
}
}
it will work.
I want to replace the <a href='http://example.org/'>this word</a> element. But the problem is that "this word" can be any word.
<?php
$link = "http://example.com";
$site = file_get_contents($link);
$ades = "<a href='http://example.org/'>this word</a>";
$bdes = "";
$site = str_replace($ades,$bdes,$site);
echo $site;
?>
'This word' is a variable
'This word' can be pink, blue, door etc.
How can I get it?
edited :
I just want to remove like these codes
blaasdsad
gertvb
ertvvuyrt
awceawce
8k9789k789k
and else
$ades = "<a href='http://example.org/'>this word</a>";
echo strip_tags($ades);
Just use strip_tags function to remove the html tags. The output will be a string with the color name.
More info about strip_tags Here!!!
If your question is how to access every tag of your actual page which has this form <a href='http://example.org/'>any text or word</a>, I would use preg_replace, which use a pattern to detect what to change (instead of a string).
For your string, it would render something like that:
<?php
$link = "http://example.com";
$site = file_get_contents($link);
// use a pattern
$ades = "/^<a href='http:\/\/example\.org\/'>.*<\/a>$/";
$bdes = "";
// use other function
$site = preg_replace($ades,$bdes,$site);
echo $site;
?>
Try this:
$var="blue";
$ades = "<a href='http://example.org/'>".$var."</a>";
$shortAdes=substr($ades,strpos($ades,$var),strlen($var));
echo $shortAdes;
it weill print the $var
I'll start by saying I'm fairly new to coding so I'm probably going about this the wrong way.
Basically I've got the below php function that changes urls to the page title of the url instead of a plain web address. So instead of www.google.com it would appear as Google.
<?php
function get_title($url){
$str = file_get_contents($url);
if(strlen($str)>0){
$str = trim(preg_replace('/\s+/', ' ', $str)); // supports line breaks inside <title>
preg_match("/\<title\>(.*)\<\/title\>/i",$str,$title); // ignore case
return $title[1];
}
}
?>
This is great but to implement this I have to use the below code.
echo get_title("http://www.google.com/");
However this just works on a predefined URL. What I have set up on my site at the moment is a shortcode in a html widget.
<a href='[rwmb_meta meta_key="link_1"]'>[rwmb_meta meta_key="link_1"]</a>
This shortcode displays a url/link that is input by the user in the backend of Wordpress and displays it on the frontend as a link. However I want to apply the get_title function to the above shortcode so instead of the web address it shows the page title.
Is this possible?
Thanks in advance.
for name of a url from a link you can use parse_url($url, PHP_URL_HOST);
easier way would be to have an array of links for example
$links[] = 'some1 url here';
$links[] = 'some2 url here';
then just loop your $links array with the function.
foreach($links as $link)get_title($link);
https://metabox.io/docs/get-meta-value/
try:
$files = rwmb_meta( 'info' ); // Since 4.8.0
$files = rwmb_meta( 'info', 'type=file' ); // Prior to 4.8.0
if ( !empty( $files ) ) {
foreach ( $files as $file ) {
echo $file['url'];
}
}
I am getting a link and in it there is a href... I want to know if it's a
http://[...].com/file.txt absolute domain name
or
/file.txt
a link that does not have the full URL.
How can I do this with PHP?
Use parse_url and see if you get a scheme and host. For example, this:
$url = 'http://username:password#hostname/path?arg=value#anchor';
$parts = parse_url($url);
echo $url, "\n", $parts['scheme'], "\n", $parts['host'], "\n\n";
$url = '/path?arg=value#anchor';
$parts = parse_url($url);
echo $url, "\n", $parts['scheme'], "\n", $parts['host'], "\n\n";
Produces:
http://username:password#hostname/path?arg=value#anchor
http
hostname
/path?arg=value#anchor
Live example: http://ideone.com/S9WR2
This also allows you to check the scheme to see if it is something you want (e.g. you'd often want to ignore mailto: URLs).
You can use preg_match with a regexp to test the format of the url string. You'll need to modify $myurl as necessary, probably passing it in as a variable.
<?php
$myurl = "http://asdf.com/file.txt"; // change this to meet your needs
if (preg_match("/^http:/", $myurl)) {
// code to handle
echo 'http url';
}
else if (preg_match("/^\\//", $myurl)) {
// code to handle
echo 'slash url';
}
else {
// unexpected format
echo 'unexpected url';
}
?>