/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] string replacement from db query

I am trying to manipulate string when the come for a db query.

i can seem to replace the underscores i use and then make the first letters upper case but what i can’t seem to do is replace some strings with the all upper equiv.

ex. i want to replace Fox or Cnn with FOX and CNN; this is what i have so far that doesnt seem to work with preg or ereg_replace:

[code=php]
$name_torep = array(“cnn”, “fox”);
$name_rep = array(“CNN”, “FOX”);
while($row = mysql_fetch_assoc($result))
{
foreach($row as $key => $value)
{
echo “<option value='”.$value.”‘>”;
$new_value = preg_replace($name_torep, $name_rep, ucwords(str_replace(“_”, ” “, $value)));
echo $new_value.”</option>n”;
}
}
[/code]

by using ereg_replace, i only get Cnn or Fox but not CNN or FOX – in the db, all my strings are lowercase and spaces are seperated by “_”.

? ?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@ZiplineApr 04.2006 — I don't know if you are just playing or if you are trying to make the stuff uppercase, if you are trying to make it uppercase... you can just do strtoupper()
Copy linkTweet thisAlerts:
@rch10007authorApr 04.2006 — The string coming from the db looks like: "cnn", "fox_news", "msnbc", "sports_illustrated", "etc..."

I can replace the "_" with a space and make the first letter uppercase but what i am asking is how do, in the case of "cnn", make the whole thing upper, not just the first letter?

i was trying to declare certain strings to make all uppercase, like "cnn" or "fox" but i can't make the script i gave earlier work.

?
Copy linkTweet thisAlerts:
@rch10007authorApr 04.2006 — [code=php]$new_value = preg_replace('/cnn|fox|msnbc/i', '/[A-Z]/', ucwords(str_replace("_", " ", $value)));[/code]

this prints out "/A-Z/" at every place where cnn fox or msnbc is found

how in the world do you just replace the pattern with it's upper case equivilent???
Copy linkTweet thisAlerts:
@NogDogApr 04.2006 — [code=php]
$name_torep = array("/cnn/i", "/fox/i");
[/code]
Copy linkTweet thisAlerts:
@rch10007authorApr 04.2006 — thx charles!!

man i tell you, this stuff is killing me - i'm going to bed!
Copy linkTweet thisAlerts:
@rch10007authorApr 04.2006 — just in case you are curious to what i have been doing, you can find it here: http://www.childsinc.com/rss/index.php

tell me what you think?
Copy linkTweet thisAlerts:
@bokehApr 04.2006 — tell me what you think?[/QUOTE]According to [URL=http://www.faqs.org/rfcs/rfc2616.html]the relevant RFC[/URL] use of the POST method in this instance is incorrect.
Copy linkTweet thisAlerts:
@rch10007authorApr 05.2006 — Bokeh, could you elaborate a little further about what you mean - i am trying to figure out which post method you mean.

this is how the rss page is setup:

first, there is a db where the table names are the sources, such as "cnn", "msnbc", "etc...". in each table, there are 2 fields (type) and (url). the type represents which type of feed is available at each source, (Business), (World News), (etc...) and the url is the xml page to read for the rss feed.

now, the php. first, if no source or type is selected by post then a select box is populated by querying the db for the table names. once you select the source (CNN), another select box is generated displaying the types of feeds (News), then when you select the type, i have a function that retrieves the POST['type'] and uses get_file_contents from the posted url (obtained in the db). the script then reads the url and preg_match_all each section i want to show, such as date published, time, and creates a link to the headline for the selection you made. then the results are sorted and displayed.

assuming i covered everything, which part is using POST incorrectly? if i am wrong, i want to fix this so could maybe post the line from http://www.faqs.org/rfcs/rfc2616.html that discuss what you mean, because that is a very long document and i'm not sure where to find the part you were talking about.

thanks
Copy linkTweet thisAlerts:
@chazzyApr 05.2006 — in simple terms, GET is designed to retrieve data - which is what your form is doing.

POST is designed to send data back to the server. Since you're not actually handling input to be saved GET is a better use of the forms.

but it's really a minute detail.
Copy linkTweet thisAlerts:
@rch10007authorApr 05.2006 — can GET handle arrays? just curious.

I am think of extending the simple rss form to one that would allow multiple enteries, such as you being able to select more than on source and type to retrieve multiple feeds.

will this make a difference in which method i use?

also, by using POST, i do want to send user input back to the server to be processed by the db, is that not right?

i want to make sure i am creating standards compliant apps so please let me know.
Copy linkTweet thisAlerts:
@bokehApr 05.2006 — You implementation of POST means a warning is raised when you click backwards to a page that sent POST data whereas, if it were to have been a GET request, there would be no error message and the page would be loaded from the cache. I find the present behaviour quite annoying.

There is a good reason why GET and POST operate in these different respective ways. POST should be used where there is a lasting effect (or an accumulated lasting effect). GET should be used in all other situations. The warning raised by reloading by the POST method is there for a reason: Let's say I just ordered a new Mercedes Benz, the order is sent by the POST method (the lasting effect is I will have a new car). If I click backwards I don't want to order a second new Mercedes Benz by accident so the browser raises a warning or refuses to let me go back.

Your page raises a warning but for no reason (there has been no lasting change). If too many people start to use POST when they should be using GET the annoying warning messages the browser produces will start to be ignored by website visitors encouraging people to just click the OK button without reading the message which in turn will will lead to lots of new Mercedes Benzes beinging ordered by accident.

Also the user interface should be transparent. Example: an online shopper is not sitting infront of a computer, he is strolling through a virtual shop of interesting products. The user interface is completely transparent. When something goes "[I]wrong[/I]" with the user interface, (a warning message), he is dragged out of the world of virtual reality and forced to think about the user interface (not a good thing).

You say you want to send info to the DB but if the info is just a record about viewing habits etc. (such as an apache log) you should still be using GET. Also just because one page can change the DB doesn't mean you should use POST on all your pages, just the one in question.

GET can handle all single part forms, arrays (using this syntax: [I]item[][/I]) and any length of data (although certain browsers only allow up to an arbitary number of characters).
Copy linkTweet thisAlerts:
@NogDogApr 05.2006 — GET also provides the benefit that the user can bookmark a result page, and when they select that bookmark the page is displayed with the desired data, instead of having to re-enter their choices via the form sequence.
Copy linkTweet thisAlerts:
@rch10007authorApr 05.2006 — all great points - i will eventually figure out how to get my script to operate correctly and i will make sure it uses get so that a record can be kept in the url for later use.
×

Success!

Help @rch10007 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...