Str replace function [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to censor words from a form string, and control the censor words with sql database. Here's what I have so far:
while ($bad_w = mysql_fetch_array($result_badw)) {
$newmessage = str_replace($bad_w['word'],"****",$org);
}
Any ideas on how to correct it?

You are repeatedly using the original string in your replacement. You should be overwriting the string
($org = str_replace($bad_w['word'],"****",$org)) to ensure all words are filtered.
Just hope nobody talks about Kuroshitsuji on your forum :p

You overwrite $newmessage each time you are replacing new word.
You should also use str_ireplace(), which is case-insensitive - that will be better for censorship.
Try like this:
$newmessage = $org;
while($row = mysql_fetch_array($result_badw)) {
$newmessage = str_ireplace($row['word'], "****", $newmessage);
}
Or, if you want the same number of * as are letters in the bad word:
$newmessage = $org;
while($row = mysql_fetch_array($result_badw)) {
$bword = $row['word'];
$repl = str_repeat('*', strlen($bword));
$newmessage = str_ireplace($bword, $repl, $newmessage);
}

Related

PHP objects with count [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can i do something like this?
class record
{
public $count;
}
$i = 0;
foreach($entry as $item) {
$i++;
$record$i = new record();
$record$i->count = $item['count'];
print $page$i;
}
Specifically, this part here is giving me errors.
$record$i = new record();
Note: the loop works fine if i just do print $item['count']
The error is: Parse error: syntax error, unexpected T_VARIABLE
see http://3v4l.org/lB4sR
${'record'.$i};
you can create a string that holds a varname and use it as a variable later - see the example
**Edit: but like #h2ooooooo sais in the comment to your question: use an array.
Hope is that what you asking, but I do not understand why to use such bad methods to count...
class record
{
static $countq = 0;
public function count(){
self::$countq++;
}
}
$entry = array(1, 4, 6, 7);
$obj = new record();
foreach($entry as $item) {
$obj->count();
}
echo record::$countq;

How to get Magento Store Details in a PHP Array by Store ID? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to create a function which takes store id and return a PHP array which have the store details like store name, store code, logo, banner, name etc.
you can get a store details like this:
$store = Mage::getModel('core/store')->load($storeId);
$code = $store->getCode();
$name = $store->getName();
You can do this to see what data you can get from the store object
var_dump($store->getData())
The logo and other settings you need to get from the config section.
$logo = Mage::getStoreConfig('design/header/logo_src', $soreId);
This way you can get all the information from the config. You just need the correct path. For this you can see the name of the input field from system->configuration and section name and build the path.
Let's analyse the logo. You can find it in the Design tab and the url looks like this: 'admin/system_config/edit/section/design'. So the first part of the path is the section name design.
The field name is groups[header][fields][logo_src][value]. Just remove groups, [fields] and [value] and you get the rest of the path header/logo_src.
Try this it will work...
public function get_storedetails($store) {
$res = array();
try {
$res["store"] = Mage::app()->getStore($store);
Mage::app()->setCurrentStore($store);
$res["storeid"] = Mage::app()->getStore($store)->getStoreId();
$res["storecode"] = Mage::app()->getStore($store)->getCode();
$res["storewebid"] = Mage::app()->getStore($store)->getWebsiteId();
$res["storename"] = Mage::app()->getStore($store)->getName();
$res["storeactive"] = Mage::app()->getStore($store)->getIsActive();
$res["rooturl"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$res["storeurl"] = Mage::helper('core/url')->getHomeUrl();
$res["storelogo_alt"] = Mage::getStoreConfig('design/header/logo_alt');
$res["storefrontname"] = Mage::app()->getStore($store)->getFrontendName(); //getLogoSrc()
$res["current_url"] = Mage::helper('core/url')->getCurrentUrl();
$res["media_url1"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
$res["media_url2"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
$res["skin_url"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
$res["js_url"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);
$res["storelogo"] = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN).'frontend/default/default/'.Mage::getStoreConfig('design/header/logo_src');
$res["storeadminname"] = Mage::getStoreConfig('trans_email/ident_sales/name');
$res["storeemail"] = Mage::getStoreConfig('trans_email/ident_sales/email');
}
catch(Exception $ex) {
echo $ex;
}
return $res;
}

I`m sure is a quick fix, but it does not seem to work like this [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The problem is that the $prenume echoing works, while $comentarii doesn`t. I tried with and without quotes. If I echo $comentarii in the while loop, it echoes. I only have 1 line of text to retreive from database. Please help! Thank you !
$gaseste_elevul = "SELECT prenume_elev, comentarii
FROM elevi
WHERE kod_utilizator=1";
$gaseste_elevul_query = mysql_query($gaseste_elevul);
while($elevul = mysql_fetch_array($gaseste_elevul_query))
{
$prenume = $elevul['prenume_elev'];
$comentarii = $elevul['comentarii'];
}
if($comentarii = NULL)
{
echo "Momentan nu aveti informari pentru $prenume!";
}
else
{
echo "Mai jos aveti informarile pentru $prenume:";
echo "$comentarii";
}//sfarsit else
I also tried to do the whole if($comentarii=NULL) in the while loop, to no result.
Change
if($comentarii = NULL)
to
if($comentarii == NULL)
Your first statement sets the $comentarii to null, and that's why it doesn't echoing anything.

PHP Simple HTML DOM Getting ONLY first 5 links inside a div class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So far I have this
<?PHP include('simple_html_dom.php');
$html = file_get_html('http://www.mangastream.com/');
foreach($html->find('.side-nav') as $t)
foreach($t->find('a')as $k)
echo $k->href . '<br>';
?>
which outputs all the links from inside the class. but I just want to have the first 5 links.
find() returns an array, you can do a single find operation instead of two, and you can slice an array to the first five elements by using array_slice.
This allows you to get the first five element easily:
$ks = $html->find('.side-nav a');
foreach (array_slice($ks, 0, 5) as $k)
echo $k->href, '<br>'
;
However I suggest you take the DOMDocument based HTML parser - perhaps in compbination with SimpleXML so that you can run xpath queries on the document instead.
try that
<?PHP include('simple_html_dom.php');
$html = file_get_html('http://www.mangastream.com/');
foreach($html->find('.side-nav') as $t){
foreach($t->find('a')as $key => $k){
echo $k->href . '<br>';
if($key >= 4){
break;
}
}
}
?>

How to update/edit a JSON file using PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is my JSON
[
{
"activity_code":"1",
"activity_name":"FOOTBALL"
},
{
"activity_code":"2",
"activity_name":"CRICKET"
}
]
I need to update {"activity_code":"1","activity_name":"FOOTBALL"} to {"activity_code":"1","activity_name":"TENNIS"} based on activity_code
How can I achieve this in PHP?
First, you need to decode it :
$jsonString = file_get_contents('jsonFile.json');
$data = json_decode($jsonString, true);
Then change the data :
$data[0]['activity_name'] = "TENNIS";
// or if you want to change all entries with activity_code "1"
foreach ($data as $key => $entry) {
if ($entry['activity_code'] == '1') {
$data[$key]['activity_name'] = "TENNIS";
}
}
Then re-encode it and save it back in the file:
$newJsonString = json_encode($data);
file_put_contents('jsonFile.json', $newJsonString);

Categories