PHP - str_replace - php

I'm having some trouble using str_replace, this block below
|0460|001|CREDITO SIMPLES NACIONAL|
when I use this:
str_replace("|0460|001|CREDITO SIMPLES NACIONAL|","a", $registroC100, $replaces);
the var $replaces increase, but the text keep the same, I'm doing something wrong?
Edit:
this is how is in my code
$registroC100 = str_replace("|0460|001|CREDITO SIMPLES NACIONAL|","a",$registroC100);

str_replace not changes your variable value unless you do it using the return value of str_replace function.
Take a look at the documentation right here https://www.php.net/manual/pt_BR/function.str-replace.php
<?php
$replaces = 0;
$registroC100 = '|0460|001|CREDITO SIMPLES NACIONAL|';
$registroC100 = str_replace("|0460|001|CREDITO SIMPLES NACIONAL|","a", $registroC100, $replaces);

From your php syntax the str_replace structure and required parameters are okay.
str_replace("|0460|001|CREDITO SIMPLES NACIONAL|","a", $replaces);
It means search the string stored in variable $registroC100, find the value "|0460|001|CREDITO SIMPLES NACIONAL|", replace it with "a" and count the number of replacement in variable $replace. The problem could be from the value of the string you are searching. What is the value of the variable $registroC100. Show us the variable declaration and initialization.

Related

PHP Remove everything except numbers from string

I am using AJAX to retrieve the ID of a certain HTML element. The HTML ID is constructed like "sqlitem_1", "sqlitem_2", "sqlitem_3" etc. and each number corresponds to a record in the database.
I tried preg_replace('/\D/', '', $item);where $item is the string I need to cut, but this didn't do the trick.
Please note that preg_replace doesn't change the argument but rather returns the new value. You might wanna check the preg_replace manual.
So what you need to do is assign the returned value
$item = preg_replace('/\D/', '', $item);
instead.
This could be done either server side (PHP) or if you wanted to preserve the variable name you could use the JavaScript Split function.
JS Split Function (Client Side)
var myVar = "sqlitem_1";
var tmp = str.split("_");
and then access each element using tmp[0] etc
https://www.w3schools.com/jsref/jsref_split.asp
OR
PHP (Server Side)
$ITEMTMP=explode("_", $item);
$itemnumber=$ITEMTMP[1];
http://php.net/manual/en/function.explode.php

Is there a simple way to return WHAT is similar between 2 strings in PHP?

I've looked into the similar_text() and levenshtein() functions, but they only seem to return THAT there are similarities and the percentage of those similarities.
What I am trying to do is compare 2 strings to determine WHAT is actually similar between the two.
Basically:
<?php
$string1 = "IMG_1";
$string2 = "IMG_2";
echo CompareTheseStrings($string1,$string2); // returns "IMG_";
If this wonderful function doesn't exist, what would be the best way to accomplish this?
My end game plan is to read through a list of file names and then replace the similar text with something user defined or just remove it all together, but I don't want to replace each files unique identifier.
Reading your 'end goal' I think you're going about this completely the wrong way I think you should really be looking at str_replace
$string1 = "IMG_1";
$string2 = "IMG_2";
// you would create a loop here to insert each string
str_replace("IMG", "NEW_TERM", $string1);
If you want to remove the text altogether then just pass an empty string in as the 2nd parameter

Substitute a variable in PHP expression with its value

It is simple thing but I cannot resolve it myself.
I am trying to extract part of a string using substr:
$Text=substr($FullText, 0, 6);
Where $FullText is a varaible containing a string.
I guess substr does not like variable and needs a string in quotas.
My question is how can I substitute variable in PHP expression with its value?
Thank you very much in advance!
$text=substr($fulltext, 0, 6);
it will work, Substr function receives string in double quotes or you can put php variable in it. Try to echo this, it will work as required
You posted this code fragment in a comment to the question:
$FullText=$ExchangePart->Item(5)->nodeValue;
//actual value of the variable is: "1.6598 6.479460 5.4545"
$Text=substr($FullText, 0, 6);
//$Text=$ExchangePart->Item(5)->nodeValue;
echo $Text;
It looks like you are handling XML here. The SimpleXMLElement class (the type of the objects created by the simplexml_*() functions) is a beast with many faces. If you try to:
echo($ExchangePart->Item(5)->nodeValue);
or
print_r($ExchangePart->Item(5)->nodeValue);
it is presented as a string but, in fact, it is also an SimpleXMLElement object.
The solution to the issue is very simple: you have to explicitly convert the value you want to a string:
$FullText = (string)$ExchangePart->Item(5)->nodeValue;
Now the value of $FullText is a string; you can safely pass it to any string processing function and it will work as described in the documentation.

evaluate string with array php

I have a string like
"subscription link :%list:subscription%
unsubscription link :%list:unsubscription%
------- etc"
AND
I have an array like
$variables['list']['subscription']='example.com/sub';
$variables['list']['unsubscription']='example.com/unsub';
----------etc.
I need to replace %list:subscription% with $variables['list']['subscription'],And so on
here list is first index and subscription is the second index from $variable
.Is possible to use eval() for this? I don't have any idea to do this,please help me
Str replace should work for most cases:
foreach($variables as $key_l1 => $value_l1)
foreach($value_l1 as $key_l2 => $value_l2)
$string = str_replace('%'.$key_l1.':'.$key_l2.'%', $value_l2, $string);
Eval forks a new PHP process which is resource intensive -- so unless you've got some serious work cut out for eval it's going to slow you down.
Besides the speed issue, evals can also be exploited if the origin of the code comes from the public users.
You could write the string to a file, enclosing the string in a function definition within the file, and give the file a .php extension.
Then you include the php file in your current module and call the function which will return the array.
I would use regular expression and do it like that:
$stringWithLinks = "";
$variables = array();
// your link pattern, in this case
// the i in the end makes it case insensitive
$pattern = '/%([a-z]+):([a-z]+)%/i';
$matches = array();
// http://cz2.php.net/manual/en/function.preg-replace-callback.php
$stringWithReplacedMarkers = preg_replace_callback(
$pattern,
function($mathces) {
// important fact: $matches[0] contains the whole matched string
return $variables[$mathces[1]][$mathces[2]];
},
$stringWithLinks);
You can obviously write the pattern right inside, I simply want to make it clearer. Check PHP manual for more regular expression possibilities. The method I used is here:
http://cz2.php.net/manual/en/function.preg-replace-callback.php

Find and concatenate result in a pattern

I have a long PHP file and I want to copy all the variable names only and build an insert sql query. Is there a way where I can search for a pattern using regular expression and concatenate the find result till I collected all the variable and spit it out in a statement?
I am using TextMate and am familiar with regular expression search. Regex search result give $0,$1 and so forth argument. Do not know if this possible though. Solution in any editor will do not just text mate.
I have just too many variable (+100) don't feel like copy every single one. Here my sample file
$ID = $_POST['id'];
$TXN_TYPE = $_POST['txn_type'];
$CHARSET = $_POST['charset']
$CUSTOM = $_POST['custom'];
You could try something with get_defined_vars(). However this function also lists GLOBAL vars. You can use this snippet to remove them if you don't want them and display only the vars you defined
$variables = array_diff(get_defined_vars(), array(array()));
However this snippet generates Notices and I haven't found a way to solve them yet.
If you've only got $_POST variables you can loop through the $_POST array itself
You create the SQL programmatically while looping through the array.
My own solution is, do the inverse. It is not probably possible.
Leave only the variable names Remove all the rest. Use
[space].+ regex to remove everything that is after the variable name.
clean the file so that only variable names are left. then do a couple more find and replace to bring the variable name in the form you want.
If you're looking to match only the variable names (not the $_POST array indices), then the regular expression is pretty much provided in the PHP documentation:
\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
This will, of course, include $_POST, but that should be easy enough to remove. If not, you could do it with negative lookahead (if TextMate supports it):
\$(?!_POST($|[^a-zA-Z0-9_\x7f-\xff]))[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*

Categories