PHP: How to get only text from variable? - php

how can i get only text data from the table? Without any links and images?
while ($row = $most_rep->fetch_assoc()) {
$post_title = $row['post_title'];
$post_content = mb_substr($row['post_content'],0,120);
$guid = $row['guid'];
$post_date = $row['post_date'];
echo
"<a href='$guid' target='_blank'>"
."<div class='post_title'>$post_title</div>"."</a>"
."<a href='$guid' target='_blank'>"
."<div class='post_description'>$post_content</div>"
."<div class='post_date'>$post_date</div>"
."</a>";
}
Inside variable $post_content i have text and images and links from the post. I would like to echo only text.
Appreciate any help

use 'strip_tags' function
http://php.net/manual/en/function.strip-tags.php
for example ,
$only_text = strip_tags($post_content);

it is not the best practice but if you still insist using this kind of approach i suggest add some Delimiter to your data $row['post_content'] and use explode(" ",$row['post_content']) to get the specific text or value that you want.
Note: the explode function will return an array.

You can use PHP Filter Sanitization:
<?php
$str = "test";
$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>

Try this to remove everything except a-z, A-Z and 0-9:
$result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
If your definition of alphanumeric includes letters in foreign languages and obsolete scripts then you will need to use the Unicode character classes.
Try this to leave only A-Z:
$result = preg_replace("/[^A-Z]+/", "", $s);
The reason for the warning is that words like résumé contains the letter é that won't be matched by this. If you want to match a specific list of letters adjust the regular expression to include those letters. If you want to match all letters, use the appropriate character classes as mentioned in the comments.

Related

php regex special character

I have:
$regExge = "/(?!((<.*?)))\b(Öffnung)\b(?!(([^<>]*?)>))/s";
$replacege = "<a href=''>Öffnung</a>";
And I used preg_replace to replace Öffnung string to html <a href=''>Öffnung</a>
$content = preg_replace($regExge, $replacege, $content);
But it not working, only working with normal string.
There any way? Thank you.
You need to indicate that your pattern should cover an encoding which includes the special characters. UTF-8 is one option here, and can be indicated by using /u at the end of the pattern:
$regExge = "/(?!((<.*?)))\b(Öffnung)\b(?!(([^<>]*?)>))/su";
$replacege = "<a href=''>Öffnung</a>"; // ^^^
$content = preg_replace($regExge, "stuff", $replacege);
print $content;
<a href=''>stuff</a>
Demo
You can try this modified version.
$content = "Offnung Öffnung s;ldjfjkasÖffnung";
$regExge = "/Öffnung/";
$replacege = "<a href=''>Öffnung</a>";
$content = preg_replace($regExge, $replacege, $content);
echo $content;

I want to echo php text but not between ()

I want to echo php text but not between (). Some thing like this =
<?php
$text = "Barry(male)";
echo $text;
?>
output =
Barry
How can i do this?
You can use preg_replace to substitute whatever is between parenthes (and the parentheses themselves) with an empty string. Like this:
<?php
$text = "Barry(male)";
echo preg_replace('#\(.*\)#', '', $text);
?>
Please note: since you didn't specify your string format, I'm assuming that the parenthesized text appears just once in the string and that there aren't nested parenthes. Otherwise, this doesn't work as expected.
Something like:
$text = "Barry(male)";
$split = explode("(", $text);
echo $split[0];
// "Barry"

How do I remove html tags and other characters from a text field

The fields contains the value of : {"93":" Size:</span>XL</span>"}. I want to display only Size: XL. I have tried using the strip_tag function but have been unsuccessful. Are there any suggestions?
Try this,
Its json encoded value.
$text = '{"93":" Size:</span>XL</span>"}';
$ar = json_decode($text);
foreach($ar as $value){
echo strip_tags($value);
}
The output will be
Size:XL
Hope its fixed ..
If your tag was correct, like so:
<span>XL</span> instead of </span>XL</span>,
strip_tags() should work for you.
I verified with the following test...
$text = "<span>XL</span>";
$goodtext = strip_tags($text);
And output the results...
echo htmlentities($text)
echo "<br/>";
echo htmlentities($goodtext);
Yielded...
<span>XL</span>
XL
Your question was a bit unclear, if the field contains all there including the { brackets, then this regex in a replace should help, just replace the characters with an empty string:
/"\:"|[^A-Za-z:]|span/g
If that is not the case and you only have the second string with the spans, you could use this one:
/[^A-Za-z:]|span/g
Be warned though, it also removes spaces and only works in specific cases, eg. does not remove other tags etc.
You could apply a global regular expression match using the function preg_match_all to extract only "Size: XX" from your field.
$field = "your data fields"
$pattern = "/(Size:)<\/span>([a-z]{1,2})<\/span>/i";
preg_match_all($pattern,$field,$result,PREG_SET_ORDER);
foreach( $result as $r ) {
print($r[1]." ".$r[2].PHP_EOL);
}
Will output:
Size: XL
Size: L
...

echo characters only in one line with explode function php

here is what i want to do
i am working with php explode function trying to limit characters it prints after defined condition
{
$result=http://php.net
new line characters i don't want to print
$links =explode("://",$result);
$nows=$links[1];
echo $nows;
}
as you can see the above code will print
php.net
new line characters i don't want to print
but instead i want to stop printing after
php.net
You can replace newline characters with nothing:
$nows = str_replace("\n", "", $links[1]);
$nows = str_replace("\r", "", $nows);
echo $nows;
If you want only what is printed on the first line, try this:
$result = "php.net
and some other text";
$nows = reset(explode("\n", str_replace("\r\n", "\n", $result)));
If the part you're looking after will always be in the first line:
$result="http://php.net
new line characters i don't want to print";
$links = explode("\n",$result);
/*
$links[0] ->http://php.net
$links[1] ->new line characters i don't want to print
*/
$links =explode("://",$links[0]);
$nows=$links[1];
echo $nows;
/*
php.net
*/
Anyway , Consider giving more details about your case in order to offer a better way.
For instance , maybe regex?
Try
$nows = trim( $links[1] );
TRIM() will remove newlines among other things
Manual page
EDIT:
Well now we have the actual situation which you say is :-
$result=http://php.net</br>nameserver:ns1</br>nameserver:ns2.
Try
$t = explode( '</br>', $result );
$t1 = explode ( '://', $t[0] );
echo $t1[1];
Just as a note, if it is you that is creating this string somewhere else </br> is not a valid html tag, it should be <br> or if you are using XHTML it should be <br />.

Regex to deterime text 'http://...' but not in iframes, embeds...etc

This regex is used to replace text links with a clickable anchor tag.
#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i
My problem is, I don't want it to change links that are in things like <iframe src="http//... or <embed src="http://...
I tried checking for a whitespace character before it by adding \s, but that didn't work.
Or - it appears they're first checking that an href=" doesn't already exist (?) - maybe I can check for the other things too?
Any thoughts / explanations how I would do this is greatly appreciated. Main, I just need the regex - I can implement in CakePHP myself.
The actual code comes from CakePHP's Text->autoLink():
function autoLinkUrls($text, $htmlOptions = array()) {
$options = var_export($htmlOptions, true);
$text = preg_replace_callback('#(?<!href="|">)((?:https?|ftp|nntp)://[^\s<>()]+)#i', create_function('$matches',
'$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], $matches[0],' . $options . ');'), $text);
return preg_replace_callback('#(?<!href="|">)(?<!http://|https://|ftp://|nntp://)(www\.[^\n\%\ <]+[^<\n\%\,\.\ <])(?<!\))#i',
create_function('$matches', '$Html = new HtmlHelper(); $Html->tags = $Html->loadConfig(); return $Html->link($matches[0], "http://" . $matches[0],' . $options . ');'), $text);
}
You can expand the lookbehind at the beginning of those regexes to check for src=" as well as href=", like this:
(?<!href="|src="|">)

Categories