Jump to content

Recommended Posts

OK, im lost again

I want to pill out a section of a string from between two different tags
I want to make a couple of functions. I need them to work by inputting the opening and closing tags eg [mytag] and [/mytag] with the str

So the first function would do something like

Lets say “exreact_info” is the name of my function
[code]
$open_tag = “[mytag]”;
$close_tag =”[/mytag]”;
$str = “blah blah blah [mytag] info needed 1 [/mytag]blah blah blah[mytag]info needed 2[/mytag]blah blah blah[mytag]info needed 3[/mytag] blah blah blah”;

$tagarray = exreact_info($str , $open_tag , $close_tag);
[/code]
I hope that this could then give me something like
[code]
$tagarray[0] // info needed 1
$tagarray[1] // info needed 2
$tagarray[2] // info needed 3
[/code]
Then i need a function to put the info back into one string again as it was before but with the edited information, something like the following
[code]
$str = “blah blah blah [mytag] info needed 1 * edited [/mytag]blah blah blah[mytag]info needed 2 * edited [/mytag]blah blah blah[mytag]info needed 3 * edited [/mytag] blah blah blah”;
[/code]
Im not asking someone to make these functions for me, I wont mind if someone does but I would like to know how to go about it.

I first tried with explode and managed to get it kind of working but then found it was case sensitive and I need this to work with all variations eg [MyTaG] to work just like [mytag] etc

I hope this all makes sense I have been up all night trying to get this code Finished and I’m just about asleep now
use str_replace to deal with variations of [mytag]
something like
[code]
$open_tag = "[mytag]";
$close_tag ="[/mytag]";
$str = "blah blah blah [mytag] info needed 1 [/mytag]blah blah blah[mytag]info needed 2[/mytag]blah blah blah[mytag]info needed 3[/mytag] blah blah blah";

$var_open = array('[MyTag]','[myTag]','[MYTAG]');// include other variations
$var_close = array('[/MyTag]','[/myTag]','[/MYTAG]');// include other variations

$limit = count($variations);
for($ctr=0;$ctr<$limit;$ctr++)
{
   $str = str_replace($variations[$ctr],$open_tag,$str);
   $str = str_replace($variations[$ctr],$close_tag,$str);
}  
[/code]

then use explode to separate into chunks, retrieve the text in between the tags
edit them, then use the implode function to join them up
something like:
[code]
$str_temp = explode('[mytag]',$str);
$ret_val = "";
$limit = count($str_temp);
for($ctr=0;$ctr<$limit;$ctr++)
{
    if (strpos($str_temp[$ctr],$close_tag)===false)
    {
        $ret_val .= $str_temp[$ctr];    
    }
    else
    {
        $tmp = explode($close_tag,$str_temp[$ctr]);
        $tmp[0] .= '(edited)'; // do your edit part here, $tmp[0] contains the text between the tags
        $ret_val .= $open_tag . implode($close_tag,$tmp); //join them up, append the open tag at the beginning
    }    
}
[/code]


hope that helps
thanks for your quick reply on this

I never thought of that

I do have one question though

If i wanted to have many different tags and be able to extract them in this way is there any way I can go throught a string and lowercase anything between [ and ] ?

this would then fix all my problems :)
I Thought I would let you all know how i fixed this problem, I made two functions one to extract the data.
[code]
function extract_from_tags($str, $open_tag, $close_tag){
    $findopenings = explode($open_tag, $str);
    foreach($findopenings as $find_closings){
        $taginfo = explode($close_tag, $find_closings);
        if (trim($taginfo[1]) == ""){
            $after_tag[] = $taginfo[0];
            $tag_info[] = " ";
        } else {
            $tag_info[] = $taginfo[0];
            $after_tag[] = $taginfo[1];        
        }
    }
    $str = array($tag_info, $after_tag);
    return $str;
}
[/code]

and one to place it back again

[code]
function replace_to_tags($tag_info, $outer_tag, $open_tag, $close_tag){
    $str = "";
    $info_count = count($tag_info);
    for ($i=0; $i<$info_count; $i++){
        if ($i == 0){
            if (trim($tag_info[$i]) == ""){
                $tag_first = false;
            } else {
                $tag_first = true;
            }
        }
        if ($tag_first = true){
            if (trim($tag_info[$i]) == ""){
                $str .= $tag_info[$i].$outer_tag[$i];
            } else {
                $str .= $open_tag.$tag_info[$i].$close_tag.$outer_tag[$i];            
            }
        } else {
            if (trim($tag_info[$i]) == ""){
                $str .= $outer_tag[$i].$tag_info[$i];
            } else {
                $str .= $outer_tag[$i].$open_tag.$tag_info[$i].$close_tag;            
            }        
        }
    }
    return $str;
}
[/code]

I know its far from perfect code but it seems to work but here is how it works


[code]
$str = "[MYTAG]INFO NEEDED 1[/mytag]between 1 and 2[mytag]info from tag 2[/MYTAG]BETWEEN 2 AND £[mytag]info from tag 3[/mytag]from the end";
$open_tag = "["; //  opening tag
$close_tag = "]"; // closing tag
[/code]

then call the function with

[code]
$str = extract_from_tags($str, $open_tag, $close_tag);
[/code]

this gives you a multidimensional array

all the info between the tags would be on
$str[0]

and the rest would be on

$str[1]
so you could call it like $str[1][1] or $str[1][2] etc
this means I can edit them and then place them back into the array and run the following

[code]
$tag_info = $str[0];
$outer_tag = $str[1];
Str = replace_to_tags($tag_info, $outer_tag, $open_tag, $close_tag);
[/code]

To get my desired output

I hope this helps anyone else that has a similar problem

It seems to of solved all my problems so far making it easy to extract what ever I want and place it back were it was before

Critics welcome, after all I’m only learning and as I said I know its far from perfect

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.