0

I am inserting a group of strings into my table. This is a sample one,

|=|4|=|3|=|5|=|3|=|Yes|=|No comments .

Sometimes my string may be like

|=|4|=|3|=|5|=|3|=|Yes|=|

For this situation I want to append a string (sorry, no data) to the end of |=|.

Also my string may be like

 |=|4|=|3|=|5|=|3|=|Yes|=| 

That symbol at the end is empty space. In this situation I also want to append this string (sorry, no data) at the end of |=|.

Can you tell me how to do this?

2
  • What table do you mean? A database table? Commented Mar 10, 2010 at 12:17
  • It’s really hard to get what you’re saying. Maybe you should try to be more descriptive. Commented Mar 10, 2010 at 13:16

4 Answers 4

2

Use susbstr to extract end of string

if (substr($string, -3) == '|=|') 
    $string .= '(Sorry no data)';

Or with one space character

if (substr($string, -1) == ' ') 
        $string .= '(Sorry no data)';

You can also achieve same thing with regular expressions

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

$splitted = explode("|=|", $your_string);
if (count($splitted) < 6)
{
  $your_string .= "Sorry no data";
}

If you want to add "Sorry no data" also if the last section is "No comments", change the if to:

if (count($splitted) < 6) || $splitted[5] == "No comments")

Comments

1

The question is, why do you need to do this? For me, it sounds like the structure of your database is wrong, and you are trying to save n variables into 1 field (if I'm right, take a look at Database normalization, if I'm wrong I missunderstood your question...).

Comments

0
$string = "|=|4|=|3|=|5|=|3|=|Yes|=|";
$var = explode("|=|",$string);
$pick_last_array = count($var);
$pick_last_array = $pick_last_array-1;
if(trim($var[$pick_last_array])==""){
    $my_last_val =  "Am coming";
}else{
    $my_last_val =  $var[$pick_last_array];
}

echo $my_last_val;

This snippet fixed my problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.