Forum Moderators: coopster

Message Too Old, No Replies

preg replace help needed

         

omoutop

10:26 am on May 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi all...

I have come to a minor problem for some, but to a hell of a problem for me.

One typical form submits some data to a page. All data are saved to db at first and show to the user later on.

Now the tricky part. If user is not logged in, he should not see some of the data... I will have him see somethig like: Phone Number: N/A

I check the login function and if I find him not logged in I simply change the data I show on screen.

For this purpose I need to use the preg_replace function.

In most cases I inject two sets of (@@) in the strings I plan to change, and using the preg_replace I intent to chage the data between the @@'s into the N/A or anything else i feel like for that data.

So what is a simple syntax for such an expression? I managed to inject the set of @@ to the various string, and I also managed to change one type of field into the N/A part. But for all the rest I failed.

I have no knowledge on regular expressions and any help is appreciated.

For anyone that may find it usefull, the pattern I use for email conversion is this: [\w-\d]+@[\w-\d]+(\.[\w-\d]+)*.

Also the above pattern leaves the @@ in the middle of the string. How can I remove them?

I try using a simpel pattern like: ^(@@(.*)@@)*$ but it failed.

Thanks in advanced

henry0

11:32 am on May 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I will do it differently:
Since you check if the user is login
You could change your loop
By something in the line of:
if($_SESSION[‘whatever’])
{
// display such and such
}
And if(!$_SESSION[‘whatever’];)
{
// display filtered data
}

the above is over simplified but you could see where I go with that.
Another word check user status and display content accordingly

omoutop

11:42 am on May 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That is my method so far (for all previous projects)... but in this new project we use some ready made CMS which is pretty messy... don't ask.

So i turned to the preg option as a last resort.
It seems the time has come to learn some regular expressions.

henry0

1:10 pm on May 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at: ereg_replace() [us.php.net]

coopster

4:02 pm on May 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm not certain what isn't working for you all the time, seems like a straightforward regular expression. Find a double at sign followed by anything followed by another double at sign. Like this:
<pre> 
<?php
$subject = '
Here is some data:
First Name: John
Last Name: Doe
Password: @@secret@@
Phone: @@555-1212@@
Email: @@name@example.com@@
';
print $subject;
$pattern = "/(@@.*@@)/";
$replacement = 'N/A';
print preg_replace($pattern, $replacement, $subject);
?>
</pre>

omoutop

5:57 am on May 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



coopster you get it right.
that's exactly the format.

But for some reason, the final result is:
First Name: John
Last Name: Doe
Password: @@N/A@@
Phone: @@N/A@@
Email: @@N/A@@

where N/A is the text I use to replace the specific fields.

I will have another go this weekend with the regular expressions and see what i can come up with.