Get PATH instead of URL [duplicate] - php

This question already has answers here:
File path without domain name from wp_get_attachment_url()
(4 answers)
Closed 10 months ago.
This code:
$files[$file->ID]['file_title'] = '<p><a target="_blank" href="' . esc_url( wp_get_attachment_url( $file->ID ) ) . '">' . $real_title . '</a>' . $title . '</p>';
Gives me:
https://example.com/wp-content/uploads/2022/03/sample.pdf`
But I want:
/wp-content/uploads/2022/03/sample.pdf`
How do I remove https://example.com/?

You can use parse_url to get the path information from a URL:
parse_url(wp_get_attachment_url($file->ID) , PHP_URL_PATH)
https://3v4l.org/5KmsJ
Alternatively str_replace or preg_replace could be used to remove https://example.com/ that those can result in incorrect results.

Related

Parse error: syntax error, unexpected '"' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I can's seem to figure out why I'm getting this error.
I need to turn user_link part into hyperlink(right now it outputs a text on the frontend). Error happens on this line in my shortcode.php file. I guess there is an error with a href part:
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' <a href='" . $order->user_link . "'>" . $order['user_link'] . "</a> </td>';
Your issue is right here:
. ' <a href='" .
Notice how you're starting with a single quote and ending with a double.
Definitely consider using an IDE. It will make these easy bugs blatantly obvious. I highly recommend PHPStorm, or if you don't want to spend anything, sublime text (Not an IDE but will provide linting and highlighting).
On top of that I'd recommend using some type of templating engine eventually. You should always try to avoid writing HTML as strings.
$output .= '<td>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' ' . $order['user_link'] . ' </td>';
You switched the ' and " in the a href. I also changed the two " to ' in the a description.

combine 2 strings in one with some elements [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
The code looks like this:
$link .= '/'. . 'elevi' . .'/'. $clasa . '/' . $litera . '/' . $nume .'-'. $prenume;
But the error apear :
Parse error: syntax error, unexpected '.' in ...
But if the code looks like this :
$link .= $clasa . '/' . $litera . '/' . $nume .'-'. $prenume;
Works like this :
11/c/bobo-alex
and i want it to looks like this
./elevi/11/c/bobo-alex
with [dot] and slash first
You have to many string combining dots in your first segment.
You can also use "" instead of '' for PHP to auto-echo variable contents to the string.
$link .= "./elevi/$clasa/$litera/$nume-$prenume";
The dots are used to concatenate two strings, so you can't put two dots consecutive, it will rise a syntax error, so you can do it in two ways:
$mystring = './' . $var1 . '/' . $var2 . '/' . $var3 . '-' . $var4;
or:
$mystring = "./$var1/$var2/$var3-$var4";

Add a word in php code [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 7 years ago.
in this code, I need to add a simple text "from" after the calendar icon.
How can i do?
Thank you
$html .= '<span class="dashicons dashicons-calendar"></span>' . $datetime->date_range( $date_format ) . '<br/>';
You can do it like this:
$html .= '<span class="dashicons dashicons-calendar"></span>'.'From'. $datetime->date_range( $date_format ) . '<br/>';

Display specific amount of text with PHP [duplicate]

This question already has answers here:
Get first 100 characters from string, respecting full words
(18 answers)
Closed 9 years ago.
I am using the following PHP code to display a set of ellipses directly after 220 characters have displayed on-screen from the description field in a MySQL database.
...echo "<td>"; echo '<a title="'.$row['title_tag']. '" href="' . $row['hyperlink'] . '">' . substr($row['description'], 0, 220) . " . . ." . '</a>'; echo "</td>";...
It works, but unfortunately this can cut off in the middle of a word. Is there a simple way in the code that I have used above to get it to cut at the next available space or end of word?
$string = preg_replace("/[^ ]*$/", '', substr($string, 0, $length));
This will cut a string after $length and then cut it to the end of the last word
Save potentially a load of wasted data being pulled out of your database, change your query to :
SELECT .... LEFT(description, 220) as description
WHERE .... etc
Then apply the tricks in previous answers using PHP on the string to only show up to the last space.

regular expression to parse URLs to links, but only if they are not links yet [duplicate]

This question already has an answer here:
PHP autolink if not already linked
(1 answer)
Closed 9 years ago.
We use the following regular expression to convert URLs in text to links, which are shortened with ellipsis in the middle if they are too long:
/**
* Replace all links with <a> tags (shortening them if needed)
*/
$match_arr[] = '/((http|ftp)+(s)?:\/\/[^<>\s,!\)]+)/ie';
$replace_arr[] = "'<a href=\"\\0\" title=\"\\0\" target=\"_blank\">' . " .
"( mb_strlen( '$0' ) > {$maxlength} ? mb_substr( '$0', 0, " . ( $maxlength / 2 ) . " ) . '…' . " .
"mb_substr( '$0', -" . ( $maxlength / 2 ) . " ) : '$0' ) . " .
"'</a>'";
This is working. However, I found that if there is a link in the text already, like:
$text = '... http://www.google.com ...';
it will match both URLs, so it will try to create two more <a> tags, totally messing up the DOM of course.
How can I prevent the regex from matching if the link is already inside an <a> tag? It will also be in the title attribute, so basically I just want to skip every <a> tag completely.
The simplest way (with a regex, which arguably is not the most reliable tool in this situation) would probably be to make sure that no </a> follows after your link:
#(http|ftp)+(s)?://[^<>\s,!\)]++(?![^<]*</a>)#ie
I'm using possessive quantifiers to make sure that the entire URL will be matched (i. e. no backtracking in order to satisfy the lookahead).

Categories