baig772
baig772

Reputation: 3488

How to get some part of text from a large text

Is there any way to get get a small part of data from an array index in php?
For example, I have an array of data like:

Array(

[title] => My title

[description] =>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type
Code: No Promo Code Required Begin: 2014-07-25 00:00:00 Expire: 0000-00-00 00:00:00

[link] => http://google.com )

from the above array's description index, i need the part only containing

Code: No Promo Code Required Begin: 2014-07-25 00:00:00 Expire: 0000-00-00 00:00:00

Regardless of where ever this part occours in the text data.

Upvotes: 0

Views: 72

Answers (3)

You can use mb_substr()

echo mb_substr($myArray['description'], 0 /* that is the indent*/, 30 /that is the count of elements which you want to show/, 'utf-8' /type of the coding/);

Upvotes: 0

Patrick Q
Patrick Q

Reputation: 6393

$myArray = array(
    "title" => "My Title",
    "description" => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type
Code: No Promo Code Required Begin: 2014-07-25 00:00:00 Expire: 0000-00-00 00:00:00",
    "link" => "http://www.gopjn.com/t/3-79115-101746-99698"
    );

preg_match("/(Code.*Expire:\s\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/", $myArray["description"], $matches);

echo $matches[1];

DEMO

If there could be more than one instance of the string that you want to match, and you want to get all of them, use preg_match_all() instead.

Upvotes: 2

mable
mable

Reputation: 144

It may not be the most direct nor best way (regex would probably be a much better idea). But one way to do it:

$foo = explode('Code:', $myarray['description'], 1);
echo array_pop($foo);

Or perhaps:

$p = stripos($myarray['description'], 'Code:');
$foo = substr($myarray['description'], $p);
echo $foo; 

Upvotes: 1

Related Questions