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/>';
Related
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.
This question already has answers here:
PHP str_replace any number pattern
(3 answers)
Closed 1 year ago.
This question is NOT a duplicate and should not be marked as such!
I have done my research and have not found a solution. Standard replacements are not a solution here (How do I replace certain parts of my string? is not helpful). It is very annoying that every question asked on Stack Overflow is marked as duplicate and closed directly after less than 5 minutes. Obviously without even having read/understood the question.
Back to the problem:
Here's an example:
<?php
$string = '<span class="X">001</span>';
$string .= '<span class="X">444</span>';
$string .= '<span class="X">242</span>';
$string .= '<span class="X">334</span>';
?>
Now I want to replace every occurrence from <span class="X"> to <span> (including the ! unknown ! number contained) with another string, for example <div title="yay">Icecream</div>.
So that the result could be:
<?php
echo $string;
// outputs:
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
// <div title="yay">Icecream</div>
?>
How can this be achieved?
For anyone facing the same problem, here's a possible answer:
<?php
$string = '<span class="X">001</span>';
$string .= '<span class="X">444</span>';
$string .= '<span class="X">242</span>';
$string .= '<span class="X">334</span>';
$replace = preg_replace('#<span class="X">.*</span>#m', '<div title="yay">Icecream</div>', $string);
echo $replace;
?>
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 5 years ago.
I would like to change all href's in a website.
xyz …
to:
<a href="sitename.html>xyz …</a>
Thanks for help!
You could do it using the following way ...
$hrefs = 'xyz …
xyz …';
$r = '/(?<=href=").*?(?=">)/';
$sitename = 'sitename.html';
$result = preg_replace($r, $sitename, $hrefs);
echo $result;
DEMO
$content = file_get_contents('page.html');
$new_content = str_replace('<a href="', '<a href="sitename.html', $content);
echo $new_content;
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php regexp: remove all attributes from an html tag
$input = '<div style="font-size: 18px; line-height: 20px;" id="whatever">text</div>';
// ... some code here, probably regex
$desired_output = '<div id="whatever">text</div>';
How do I do the above using PHP?
try the following you need a regular expression to strip it out.
$desired_output = preg_replace('/<\s*style.+?<\s*\/\s*style.*?>/si', ' ', $input );
or this
$desired_output = preg_replace('%style="[^"]+"%i', '', $input);