Skip to content
Advertisement

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.

Advertisement

Answer

$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:sd{4}-d{2}-d{2}sd{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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement