Remove ellipsis (&helip;) after sub-string concatenation - php

I am concatenating two sub-strings with an ellipsis (...) added at the end of the first sub-string. However I want this elipsis to be removed after the concatenation.
Is this possible via a script or other means, Thanks:
<?php echo substr($post_text_result2, 0, 400) . "…";?><div id="second_post" class = "hidden"><?php echo substr($post_text_result2, 400, 5000);?></div>:
str_replace deletes the elipsis like requested, however does not fully work in my situation:
The code below works however, The first sub-string is repeated. I need a way to remove the first sub-string.
<?php
$string = substr($post_text_result2, 0, 400) . "…";
echo $string;
?>
<div id="second_post" class = "hidden">
<?php
$string= str_replace('…','',$string); echo $string;
echo $string;
echo substr($post_text_result2, 400, 5000);
?>

easy
<?php
$string = substr($post_text_result2, 0, 400) . "…";
$string = str_replace('…','',$string);
?>
If you want an ellipsis always at the end, just do this:
<?php
$string = substr($post_text_result2, 0, 400) . "…";
$string = str_replace('…','',$string) . "…";
?>

You can store the length of the first string A and then build a new string concatenating 2 substrings:
substring 1 from 0 until length(A) - 3
substring 2 from length(A) until total length
You also could use a regex and replace "..." with "", but that could remove other "..." which is maybe unwanted.
Sorry for no code, I can't PHP but you know how to do it ;)

Related

Laravel get column length and turn into spaces [duplicate]

I want to like add space in sting with specified string length.
For example : Hello is my string & i set string length 30 so next string should be start after length 30 so it should be like Hello Next string.
I would like to add space..
I have used following code but it's not adding space it's take whole character but not added space.
<?php
$fistString ="Hello World";
$secondString ="Good";
echo str_pad($fistString, 45, ' ', STR_PAD_RIGHT).$secondString;
?>
it will be done by using this
$string_1 = "The quick brown fox jumped over the lazy dog.";
$string_2= " -space-or-anything- ";
$newtext = wordwrap($string_1, 20, " $string_2 <br>");
// it will add second string after every 20th character and also break the line
echo $newtext;
// result will be like
The quick brown fox -space-or-anything-
jumped over the lazy -space-or-anything-
dog.
as per my knowledge the answer is :-
<?php
$fistString ="Hello World";
$secondString ="Good";
$str=$fistString;
for ($i=0; $i < 30; $i++) {
$str.=' ';
}
$str.=$secondString;
echo $str;
?>
I did with my self with following way..
<?php
echo str_pad($string, 30, " &nbsp", STR_PAD_RIGHT).$secondString;
?>
Add space with specified string length in php
$temp ="optionA"
chunk_split($temp ,6,' ')
output : option A

How to add "..." to the end of product title, php echo

I'm trying to add "..." after my product title.
<a href="index.php?section=1&show=<?php echo $row['id_product']; ?>">
<?php
$title = $row['name'];
$newtext = str_pad($title, 6, "...");
echo $newtext; ?>
</a>
But php print only the $row['name'] and doesn't add "..."
How is it possible to make this happen?
From str_pad() docs
If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned.
You can get the length first of your string then just add the length of the string to pad. In your case,
$title = $row['name'];
$length = strlen($title) + 6;
$newtext = str_pad($title, $length, "...");
echo $newtext;
For cutting the string to a smaller string, then adding "...". You can use substr() then concatenate. With additional check for more than 6 string length
$title = $row['name'];
$newtext = strlen($title) > 6 ? substr($title, 0, 6) . "..." : $title;
echo $newtext;

Determine if it's end of a string in PHP

Is there a way to determine if it has reached the end of a string or not.
Right now I'm using this PHP function
<?php echo substr('some text', 0, 7)?>
To limit the character number to be displayed. Now I would like to add "..." to the text but only if it's not the end of the string.
So when I have a text "Hello!" it would display the whole thing and if I have a text "Determine" it would not only display "Determi" but also adds this "..." like that "Determi...".
I've tried these ellipse and overflow ways but they didn't work for me.
Would something like this described above be possible?
You can achieve this with the built-in function mb_strimwidth:
<?php
echo mb_strimwidth("some text", 0, 7, "...")
// output: "some te..."
?>
You could do something like this :
<?php echo (strlen($text) > 7 ? (substr($text, 0, 7) . '...') : $text) ?>
If string has more than 7 characters, you echo only the first 7 ones and add ..., otherwise you just echo the text
function trimStr($str, $len = 7) {
return strlen($str) > $len ? substr($str, 0, $len) . '...' : $str;
}

PhP Find (and replace) string between two different strings

I have a string, that look like this "<html>". Now what I want to do, is get all text between the "<" and the ">", and this should apply to any text, so that if i did "<hello>", or "<p>" that would also work. Then I want to replace this string with a string that contains the string between the tags.
For example
In:
<[STRING]>
Out:
<this is [STRING]>
Where [STRING] is the string between the tags.
Use a capture group to match everything after < that isn't >, and substitute that into the replacement string.
preg_replace('/<([^>]*)>/, '<this is $1>/, $string);
here is a solution to test on the pattern exists and then capture it to finally modify it ...
<?php
$str = '<[STRING]>';
$pattern = '#<(\[.*\])>#';
if(preg_match($pattern, $str, $matches)):
var_dump($matches);
$str = preg_replace($pattern, '<this is '.$matches[1].'>', $str);
endif;
echo $str;
?>
echo $str;
You can test here: http://ideone.com/uVqV0u
I don't know if this can be usefull to you.
You can use a regular expression that is the best way. But you can also consider a little function that remove first < and last > char from your string.
This is my solution:
<?php
/*Vars to test*/
$var1="<HTML>";
$var2="<P>";
$var3="<ALL YOU WANT>";
/*function*/
function replace($string_tag) {
$newString="";
for ($i=1; $i<(strlen($string_tag)-1); $i++){
$newString.=$string_tag[$i];
}
return $newString;
}
/*Output*/
echo (replace($var1));
echo "\r\n";
echo (replace($var2));
echo "\r\n";
echo (replace($var3));
?>
Output give me:
HTML
P
ALL YOU WANT
Tested on https://ideone.com/2RnbnY

Delete tag html on PHP

How to delete tag html on php
my data:
$a="<div style='color: blue;'>Hallo</div>
<div>yas!</div><img src='blabla/aa/img.png'> im fine
<span> yes</span> <a href='aaa.com'>link</a>";
How to delete all div and span
<div style='color: blue;'></div>, <div></div> and <span></span>
and not delete
<img src='blabla/aa/img.png'> and <a href='aaa.com'>link</a>
Please help me
How about using strip_tags:
$a="<div style='color: blue;'>Hallo</div>"
."<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";
$clean_html = strip_tags($a, "<img><a>");
If you have a white list of element then you can use the strip_tags() function http://php.net/manual/en/function.strip-tags.php.
Another option is to use http://htmlpurifier.org
WhiteList
http://htmlpurifier.org/live/configdoc/plain.html#HTML.AllowedElements
Blacklist
http://htmlpurifier.org/live/configdoc/plain.html#HTML.ForbiddenElements
Your sentence cant explain what is your goal. Have a look at this.
substr — Return part of a string
Description
string substr ( string $string , int $start [, int $length ] )
Returns the portion of string specified by the start and length parameters.
Parameters
string
The input string. Must be one character or longer.
start
If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.
If start is negative, the returned string will start at the start'th character from the end of string.
If string is less than or equal to start characters long, FALSE will be returned.
Examples
Example - Basic substr() usage
<?php
echo substr('abcdef', 1); // bcdef
echo substr('abcdef', 1, 3); // bcd
echo substr('abcdef', 0, 4); // abcd
echo substr('abcdef', 0, 8); // abcdef
echo substr('abcdef', -1, 1); // f
// Accessing single characters in a string
// can also be achieved using "square brackets"
$string = 'abcdef';
echo $string[0]; // a
echo $string[3]; // d
echo $string[strlen($string)-1]; // f
?>
Or
If you want to remove HTML tags and only allow some of them you can use strip_tags function like this
$a="<div style='color: blue;'>Hallo</div>"."<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";
$clean_html = strip_tags($a, "");
This will return the text you entered $text with no tags except & tags You can read more about strip_tags
Source : http://us2.php.net/manual/en/function.substr.php
You Can use this:
<?php
$a="<div style='color: blue;'>Hallo</div><div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";
echo strip_tags($a,"<img><a>");
?>
http://php.net/manual/en/function.preg-replace.php
$a="<div style='color: blue;'>Hallo</div>
<div>yas!</div><img src='blabla/aa/img.png'> im fine<span> yes</span>";
$find=[
"/<div style='color: blue;'>.*.<\/div>/",
"/<div>.*.<\/div>/",
"/<span>.*.<\/span>/"
];
echo preg_replace($find,"",$a);

Categories