Display specific amount of text with PHP [duplicate] - php

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.

Related

Convert *text* into <span>text</span> with function [duplicate]

This question already has answers here:
Make text between asterisks bold
(3 answers)
Closed 5 years ago.
How can I take a string like this: *Up to* $1,000
and turn it into this: <span>Up to</span> $1,000
The stars can be anywhere in the string and also there can be multiple sets of stars. But each set should replaced with span's.
e.g.
text *test* here = text <span>test</span> here
text here *test* right *now* = text here <span>test</span> right <span>now</span>
I need to be able to pass the value into a function and receive the formatted string in return. Thanks ahead of time.
Simple regex can do this:
function replace_star($str) {
return preg_replace('~\*([^*]*)\*~ms', '<span>\1</span>', $str);
}
echo replace_star('*Up to* $1,000') . "\n";
echo replace_star('text here *test* right *now*');
Output:
<span>Up to</span> $1,000
text here <span>test</span> right <span>now</span>

Replace pattern between several string delimiters in PHP - Use of backreferences [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
I have the following string in PHP:
$string = "<img src=\"url\" >HELLO WORLD<ol>I must replace the text after img and before ol.</ol>";
print htmlentities($string);
I want to find the substring HELLO WORLD (or whatever substring is, that is just an example of a text that will be completely dynamical), using the delimiters : "<img ... >" and "<ol>" and add <h3> delimiters. So the string above would result in:
<img src="url" ><h3>HELLO WORLD</h3><ol>I must replace the text after img and before ol.</ol>
I have tried the following code, of course with no success:
$string = preg_replace("/\<img (.*?)\> (.*?)\<ol\>/", "<img (.*?)><h3> (.*?)</h3></ol>", $string);
I know how to make very easy substituions, but the above condition is very far from my understanding.
I have found the answer by trial-and-error:
$string = "<img src=\"url\" >HELLO WORLD<ol>I must find the text after img and before ol.</ol>";
$string2 = preg_replace("/<img (.*?)>(.*?)<ol>/", "<img $1><h3>$2</h3><ol>", $string);
print htmlentities($string) . " <br />" . htmlentities($string2);
Explanation: I add the delimiters, and use $1 and $2 for matching the results between the delimiters in the right order.

PHP replace hyphen with space [duplicate]

This question already has answers here:
how to replace hyphen with blank space / white space? php
(2 answers)
Closed 8 years ago.
So, I have a piece of code I am using in most of my pages which sets the page title as the file name. This is because I don't want to have to change the title each time I create a new page.
This all works fine, except I don't want to have spaces (or rather, %20) being shown in the url when the file name contains spaces. I'd much rather have hyphens in place of the spaces, as it looks cleaner to the user. However, this means PHP will set the page title also with hyphens instead of spaces, which, frankly, looks ugly.
Is there a way I can rewrite this code to replace the hyphens with spaces?
The code:
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst($path_parts['filename']);
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use str_replace to replace the - with a space.
<?php
echo '<title>';
$path_parts = pathinfo(__FILE__);
echo ucfirst(str_replace('-', ' ', $path_parts['filename']));
echo " - Tom's basic Web Tutz";
echo '</title>';
?>
You can use implode adn explode for this
$string = "the string";
$arr = explode("string to replace",$string);
$string = implode("string to add ",$arr);
echo $string;

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).

PHP How to add HTML to another HTML at a specific point? [duplicate]

This question already has answers here:
Insert a string before the end of a link
(3 answers)
Closed 9 years ago.
I have this link like so:
$link = 'do something';
Now I need to add html to the end of the text within the anchor tag so it ends up like this:
$link = 'do something<i class="arrow"></i>';
I have researched around and my conclusion is that I probably have to use regular expression but I don't know where to start. I know this can be done easily via JS but I need it done in PHP.
Any ideas?
You could use str_replace()-
$link = 'do something';
$link = str_replace("</a>", '<i class="arrow"></i></a>', $link);
Here is an example using substrings and concatenation:
$link = 'do something';
$addition = '<i class="arrow"></i>';
$formatted_str = substr($link, 0, strlen($link) - 4) . $addition . substr($link, strlen($link) - 4);
echo $formatted_str;

Categories