Jump to content

String Manipulation


b00ker_b0y

Recommended Posts

hey people;

 

got my self in a bit of a problem.

 

I read from the database a piece of text i.e.

 

"Hello one, two, three.<br><br>Click <a href='index.html'>here</a> now. Sentence 3. Setence 4."

 

I use the the following code to basicly only take the first 3 sentences:

 

    $str_arr = explode(".",$data[Content]);

        $summary = $str_arr[0].".".$str_arr[1].".".$str_arr[2];

 

so i basicly just uses the full stops as like a end of line marker, but in the second sentence as part of the hyperlink there is a full stop which is being read as one, then messing up my output.

 

Is there anyway i can ignore this full stop? or is there a way is can remove the link wothout removing the hyperlink text from the string?

 

Help would be appriated! thanks.

Link to comment
Share on other sites

you would need preg_split();

 

the regex would have to search for '.' that are not inside an a tag (you maybe want to control whether its in other tags too!!)

 

$str_arr = preg_split('/(?!<a(.)*?>)(.)*?\.(.)*?(?!<\/a/)',$str);

 

something like that (don't expect that to work straight off but someone much better at regex will correct me! ;))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.