Jump to content

Take a string from a string between two strings


aliento

Recommended Posts

Little difficult for me to explain without a paradigm :

$main_string = '<font size=12> fddsf sdf sdf sdfsdf sdf </font>';
$starts_with = '<//';
$ends_with = '>';

$final_string = magic_i_dont_know($starts_with,$ends_with,$main_string);

echo $final_string ;

result : font

 

Thats all, i dont want to take it on the plate but i cant find a way on my own as php strings functions are not providing this kind of solution.

Link to comment
Share on other sites

by this post i found the way to express myself to google :

function get_text($text, $s, $e) // Get string out of text

{

$sp = strpos($text, $s, 0) + strlen($s);

$ep = strpos($text, $e, 0);

return substr($text, $sp, $ep - $sp);

}

 

Anyway thanks

Link to comment
Share on other sites

No i didnt found the solution yet,

 

I am loading all the html page into a string.

what i need is to find if all html tags close, and if any tag is not closing then load it to an array?

Is there any way

 

One way i thought is this :

/* html tags begin */
$html_tag[]='a';$html_tag[]='applet';$html_tag[]='big';$html_tag[]='blockquote';$html_tag[]='body';$html_tag[]='caption';$html_tag[]='center';$html_tag[]='cite';$html_tag[]='code';$html_tag[]='col';$html_tag[]='colgroup';$html_tag[]='del';$html_tag[]='dfn';$html_tag[]='div';$html_tag[]='dl';$html_tag[]='em';$html_tag[]='fieldset';$html_tag[]='font';$html_tag[]='form';$html_tag[]='frame';$html_tag[]='frameset';$html_tag[]='h1';$html_tag[]='h2';$html_tag[]='h3';$html_tag[]='h4';$html_tag[]='h5';$html_tag[]='h6';$html_tag[]='head';$html_tag[]='html';$html_tag[]='i';$html_tag[]='iframe';$html_tag[]='img';$html_tag[]='ins';$html_tag[]='kbd';$html_tag[]='label';$html_tag[]='legend';$html_tag[]='map';$html_tag[]='menu';$html_tag[]='noframes';$html_tag[]='object';$html_tag[]='ol';$html_tag[]='p';$html_tag[]='pre';$html_tag[]='q';$html_tag[]='s';$html_tag[]='samp';$html_tag[]='script';$html_tag[]='select';$html_tag[]='small';$html_tag[]='span';$html_tag[]='strike';$html_tag[]='strong';$html_tag[]='style';$html_tag[]='sub';$html_tag[]='sup';$html_tag[]='table';$html_tag[]='tbody';$html_tag[]='textarea';;$html_tag[]='tfoot';$html_tag[]='th';$html_tag[]='thead';$html_tag[]='title';$html_tag[]='tt';$html_tag[]='u';$html_tag[]='ul';$html_tag[]='var';
/* html tags end */

$html_spot=$_GET['html_spot'];


for($i=0;$i<count($html_tag);$i++)
{
$close_tag = substr_count($html_spot,'</'.$html_tag[$i].'>');
$open_tag = substr_count($html_spot,'<'.$html_tag[$i]);
if ($close_tag!=$open_tag) $error[] = $html_tag;
}

 

but if at the text of the page the publisher write <body into the text then will return error.

Is there a more professional way?

Link to comment
Share on other sites

This should work.

/**
* @return string | bool
*/
function magic_i_dont_know ($starts_with, $ends_with, $main_string) {
     if (($start = strpos($main_string, $starts_with)) !== false) {
          if (($end = strpos($main_string, $ends_with, $start + 1)) !== false) return substr($main_string, $start, $end);
          return false;
     }
     return false;
}

Link to comment
Share on other sites

Mind telling me what you want to accomplish with that? Do you want to strip the HTML tags? There is a PHP function for that. It would help if you tell us what you want to do. I know you want to get the word "font" in your example, but why?

Link to comment
Share on other sites

the word string should not be used in the same sentence more than once, for that matter any word should not be used in the same sentence once! =D

 

but I'm not understand what you want to do here...

Link to comment
Share on other sites

What i am developing is a script , the html page will be load to a textarea , then the user will have the ability to copy some parts of the code to the db. But the script needs to know if the user tried to save the correct part of html , which will be with correct html tags.

I dont want to let him copy to db ex

'<html>

<body>

<a href=jjj>dffdf</a>'

 

But the script will return : error tags <html> and <body> are not closing.

I hope i made you understand

and i hope this is easy

Thank you

Link to comment
Share on other sites

Wow, next time, I suggest you state that rather than what you have in your original post. They are so different.

 

And that is such a hard task. I personally do NOT know of an easy solution other than to list common tags and loop to match them. Um... there are so many exceptions though. You probably don't want something like this -

 

'<html>

<body>

<a href="asfdasf>asfd<asf></b></a>'

 

Missing closing quotes, <asf> isn't a tag, and closing of tag </b> when it's not opened. What I would end up doing would be utilize cURL and try to scrape any errors off w3 validator. :D

Link to comment
Share on other sites

Yes you are right and thank you supporting me.

 

- I dont care if the page is correct , the user will load a page that he made it, if he did those kind of mistakes then its his problem. so lets hypothesize that the code is correct.

 

Its better to talk to you with code as my English are not so good :)

the code that works is this :

/* Those are all the html tags that opens and close */

$html_tag[]='a';$html_tag[]='applet';$html_tag[]='big';$html_tag[]='blockquote';$html_tag[]='body';$html_tag[]='caption';$html_tag[]='center';$html_tag[]='cite';$html_tag[]='code';$html_tag[]='col';$html_tag[]='colgroup';$html_tag[]='del';$html_tag[]='dfn';$html_tag[]='div';$html_tag[]='dl';$html_tag[]='em';$html_tag[]='fieldset';$html_tag[]='font';$html_tag[]='form';$html_tag[]='frame';$html_tag[]='frameset';$html_tag[]='h1';$html_tag[]='h2';$html_tag[]='h3';$html_tag[]='h4';$html_tag[]='h5';$html_tag[]='h6';$html_tag[]='head';$html_tag[]='html';$html_tag[]='i';$html_tag[]='iframe';$html_tag[]='img';$html_tag[]='ins';$html_tag[]='kbd';$html_tag[]='label';$html_tag[]='legend';$html_tag[]='map';$html_tag[]='menu';$html_tag[]='noframes';$html_tag[]='object';$html_tag[]='ol';$html_tag[]='p';$html_tag[]='pre';$html_tag[]='q';$html_tag[]='s';$html_tag[]='samp';$html_tag[]='script';$html_tag[]='select';$html_tag[]='small';$html_tag[]='span';$html_tag[]='strike';$html_tag[]='strong';$html_tag[]='style';$html_tag[]='sub';$html_tag[]='sup';$html_tag[]='table';$html_tag[]='tbody';$html_tag[]='textarea';;$html_tag[]='tfoot';$html_tag[]='th';$html_tag[]='thead';$html_tag[]='title';$html_tag[]='tt';$html_tag[]='u';$html_tag[]='ul';$html_tag[]='var';

 

/* here i load the the html code to copy it to the db */

$html_spot=$_GET['html_spot'];

 

 

/* for every html tag , find how many times the </$html_tag> appear , find how many <$html_tag> appear . if the # of  </$html_tag> =  </$html_tag> then is ok , else save to $error[] the tag */

for($i=0;$i<count($html_tag);$i++)

{

  $close_tag = substr_count($html_spot,'</'.$html_tag[$i].'>');

  $open_tag = substr_count($html_spot,'<'.$html_tag[$i]);

  if ($close_tag!=$open_tag) $error[] = $html_tag;

}

 

 

 

but it have logical bugs and i believe that it can be easier with your magic.

 

I appreciate your help

Link to comment
Share on other sites

Instead of being very wordy, you could do -

$html = array('a', 'applet', 'big', 'blockquote', ...);

 

I didn't know big is a HTML tag. I guess a better way is to use preg_match_all to loop through the string and match all tag names. So like you would end up with 2 arrays - one for tag open and one for tag close.

 

$tags_open = array('html', 'head', 'body', 'a', 'b', 'input', 'a', 'table',...);

$tags_close = array(..., 'table', 'a', 'b', 'a', 'body');

 

Filter out $tags_open to check tags like INPUT and IMG that close themselves and they do NOT have a </input> or </img> tag and remove them from the array. Then loop through tag open and see if the corresponding tag close is closing it. $tags_close array is listing it in the opposite direction, so maybe you want to reverse the array or something, but that's completely up to you.

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.