Vladislavs S
Vladislavs S

Reputation: 63

PHP: How to get only text from variable?

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

Upvotes: 0

Views: 1711

Answers (4)

Mohamad ali Zayat
Mohamad ali Zayat

Reputation: 61

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.

Upvotes: 0

Abhi D&#39;y
Abhi D&#39;y

Reputation: 1

You can use PHP Filter Sanitization:

<?php
$str = "test";

$newstr = filter_var($str, FILTER_SANITIZE_STRING);
echo $newstr;
?>

Upvotes: 0

lucky0229
lucky0229

Reputation: 36

use 'strip_tags' function

http://php.net/manual/en/function.strip-tags.php

for example ,

$only_text = strip_tags($post_content);

Upvotes: 1

AP aul
AP aul

Reputation: 358

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.

Upvotes: 0

Related Questions