Echo error data using str_replace function - str-replace

I have a problem to replace a certain format with a symbol inside the number. I use below the coding to replace, but not work for me to replace the symbol, the result won't come out.
$try = "100-1-1 abc/bbb";
$try_1 = "100-1-1-16 John/James";
echo(replaceHyphen($try));
echo(replaceHyphen($try_1));
<script>
function replaceHyphen($str) {
$i = -1;
for($i=0; $i < strlen($str); $i++) {
if($str[$i] == "-") break;
}
for($i++; $i < strlen($str); $i++) {
if($str[$i] == "-") $str[$i] = '/';
if($str[$i] == " ") break;
}
return $str;
}
</script>
I want the expected result like below:
100-1/1 abc/bbb
100-/1/16 John/James
Hope someone can guide me on how to solve it. Thanks.

Related

dart newbie progress debugging error here

main(){
dynamic input = "saya sedang belajar menjadi programmer yang handal";
List<String> huruf = input.split('');
print('input : $input');
var vokal = ['a','i','e','o','u'];
var outputk = '';
var outputv = '';
int index = 0;
do{
if (vokal.contains(huruf[index])) {
outputk += ' ';
outputv += huruf[index];
}else{
outputk += huruf[index];
outputv += ' ';
}
}while(index<huruf.length);
print('output konsonan : $outputk');
print('output vokal = $outputv');
}
why the outputk and outputv result wont come out? when i debug it just print the input
i have been told that i need to change the index but i dont know to change to what
sry for bad english btw
It looks like you just forgot to increment index in your loop. So it create an infinite loop. Adding a ++ to the while condition makes it work.
} while (++index<huruf.length);

Phpspreadsheet Search Between Columns

Here's my code.
$highestColumn++;
for ($row = 1; $row <= $highestRow; $row++) {
for ($col = 'A'; $col != $highestColumn; ++$col) {
$val = $sheet2->getCell($col . $row)->getValue();
if($col < 'D'){
//something
}
}}
In if, i'm getting A,B,C columns and getting other 'AA' 'BB' 'CC' 'CB' ... etc. And this is wrong solution for me. Is there any thing to do for this condition? I just want to see A,B,C columns in if.
I solved the problem like this:
$sheet->rangeToArray('BU3:CA3')[0];

Different Breaking in Textarea vs. Inline?

I am working on an extended Textarea like http://podio.github.com/jquery-mentions-input/
There you can see a transparent Textarea with an element in background simulating the highlighting.
You can see the problem there also: type some long text like "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii " (attention to space at the end)
and then type "#ke" and choose first contact.
You will see that the background breaks different than the text in the textarea.
I figured out that this is not because different sizes!
Any ideas how to avoid that?
P.S.: I dont want to you contentediable.
For testing i used chrome (test with points!) and firefox.
I think this technic is also used often for auto-calculating a textarea-hight and they must have the same problems?!
I found a different solution myself: count line-breaks manually.
I modified and improved the line-break-adder from this thread: finding "line-breaks" in textarea that is word-wrapping ARABIC text
The big difference: this function only retrieves the breaked value without applying the breaks cause it used a temporary element copy.
I think it could help someone else!
function getApplyLineBreaks(strTextAreaId)
{
var strRawValue = $('#' + strTextAreaId).val();
var measureClone = $('#' + strTextAreaId).clone();
measureClone.attr('id', 'value_break_mess_clone');
measureClone.val('');
measureClone.css('overflow', 'hidden');
measureClone.removeAttr('onchange').removeAttr('onclick').removeAttr('onkeydown').removeAttr('onkeyup').removeAttr('onblur').removeAttr('onfocus');
measureClone.height(10);
measureClone.insertAfter('#' + strTextAreaId);
var lastScrollWidth = measureClone[0].scrollWidth;
var lastScrollHeight = measureClone[0].scrollHeight;
var lastWrappingIndex = -1;
var tolerancePixels = 5; //sollte kleiner als font-size
var addedSpace = false;
var debug_c = 0;
for (var i = 0; i < strRawValue.length; i++)
{
var curChar = strRawValue.charAt(i);
if (curChar == ' ' || curChar == '-' || curChar == '+')
lastWrappingIndex = i;
measureClone.val(measureClone.val() + curChar);
addedSpace = false;
if (i != strRawValue.length - 1 && strRawValue.charAt(i + 1) != "\n")
{
measureClone.val(measureClone.val() + ' '); //this is only 90% zero-width breaker unnoticed
addedSpace = true;
}
if (((measureClone[0].scrollWidth - tolerancePixels) > lastScrollWidth) || ((measureClone[0].scrollHeight - tolerancePixels) > lastScrollHeight))
{
if (addedSpace)
measureClone.val(measureClone.val().substr(0, measureClone.val().length - 1));
var buffer = "";
if (lastWrappingIndex >= 0)
{
for (var j = lastWrappingIndex + 1; j < i; j++)
buffer += strRawValue.charAt(j);
lastWrappingIndex = -1;
}
buffer += curChar;
measureClone.val(measureClone.val().substr(0, measureClone.val().length - buffer.length));
if (curChar == "\n")
{
if (i == strRawValue.length - 1)
measureClone.val(measureClone.val() + buffer + "\n");
else
measureClone.val(measureClone.val() + buffer);
}
else
{
measureClone.val(measureClone.val() + "\n" + buffer);
}
lastScrollHeight = measureClone[0].scrollHeight;
}
else if (addedSpace)
{
measureClone.val(measureClone.val().substr(0, measureClone.val().length - 1));
}
}
var returnText = measureClone.val();
measureClone.remove();
return returnText;
}
Only thing: its slow on long texts. Ideas for optimization are welcome.

.net Razor alternative to PHP echo

I'm new to .net MVC and Razor engine but I have been using PHP for a long time. I'm trying to do this PHP code in Razor:
var data = [
<?php for ($i = 0; $i < 50; ++$i) {
echo '[' . $i . ',' . sin($i) . ']';
if ($i != 49)
echo ',';
?>
],
I managed to do it using this, but it looks bad and complex for something so simple
var data = [
#for(int i = 0; i < 50; ++i) {
<text>[</text>#i<text>,</text>#Math.Sin(i)<text>]</text>if (i != 49) {<text>,</text>}
}
];
The problem is that [, ] and , are confused with Razor syntax and gives syntax errors, so I had to wrap them on <text> tags.
Is there a simpler/nicer way to do this? Maybe something like the PHP echo.
Thanks.
A gerenal equivalent of echo in MVC cshtml might be:
#Html.Raw("SomeStringDirectlyInsideTheBrowserPageHTMLCode")
This renders the (dynamic) string at its position where '<' and '>' no need to be HTML-coded. For example #Html.Raw(String.Format("<div class=\"{0}\" style=\"{1}\">", myclass, mystyle)) works fine.
Note that the HTML tags rendered by #Html.Raw(MyString) cannot be checked by the compiler. I mean: #Html.Raw("<div ....>") cannot be closed by mere </div> because you will get an error (<div ....> is not detected by the compiler) so you must close the tag with #Html.Raw("</div>")
P.S.In some cases this doesn't work (for example it fails within DevExpress) - use ViewContext.Writer.Write() or ViewContext.Writer.WriteLine() instead.
Use this:
#String.Format("[{0},{1}]", i, Math.Sin(i))
And for comma you can use String.Join() if you create array (String.Join Method )
Old question I know, but an alternative is to use the plaintext syntax #:
var data = [
#for (int i = 0; i < 50; ++i)
{
#:[#i,#Math.Sin(i)]
#(i != 49 ? "," : "")
}
];
Constructing a JSON object using a view is not really the best way to go about it. You can use the native JSON support to do this directly from a controller, for example:
public JsonResult SinArray()
{
return new JsonResult() {
Data = Enumerable.Range(0, 50).Select(i => new[] { i, Math.Sin(i) }),
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
This returns
[[0,0],[1,0.8414709848078965],[2,0.90929742682568171],.....,[49,-0.95375265275947185]]
As a bonus you get the correct content-type.

How can I properly parse an email address with name?

I'm reading email headers (in Node.js, for those keeping score) and they are VARY varied. E-mail addresses in the to field look like:
"Jake Smart" <jake#smart.com>, jack#smart.com, "Development, Business" <bizdev#smart.com>
and a variety of other formats. Is there any way to parse all of this out?
Here's my first stab:
Run a split() on - to break up the different people into an array
For each item, see if there's a < or ".
If there's a <, then parse out the email
If there's a ", then parse out the name
For the name, if there's a ,, then split to get Last, First names.
If I first do a split on the ,, then the Development, Business will cause a split error. Spaces are also inconsistent. Plus, there may be more e-mail address formats that come through in headers that I haven't seen before. Is there any way (or maybe an awesome Node.js library) that will do all of this for me?
There's a npm module for this - mimelib (or mimelib-noiconv if you are on windows or don't want to compile node-iconv)
npm install mimelib-noiconv
And the usage would be:
var mimelib = require("mimelib-noiconv");
var addressStr = 'jack#smart.com, "Development, Business" <bizdev#smart.com>';
var addresses = mimelib.parseAddresses(addressStr);
console.log(addresses);
// [{ address: 'jack#smart.com', name: '' },
// { address: 'bizdev#smart.com', name: 'Development, Business' }]
The actual formatting for that is pretty complicated, but here is a regex that works. I can't promise it always will work though. https://www.rfc-editor.org/rfc/rfc2822#page-15
const str = "...";
const pat = /(?:"([^"]+)")? ?<?(.*?#[^>,]+)>?,? ?/g;
let m;
while (m = pat.exec(str)) {
const name = m[1];
const mail = m[2];
// Do whatever you need.
}
I'd try and do it all in one iteration (performance). Just threw it together (limited testing):
var header = "\"Jake Smart\" <jake#smart.com>, jack#smart.com, \"Development, Business\" <bizdev#smart.com>";
alert (header);
var info = [];
var current = [];
var state = -1;
var temp = "";
for (var i = 0; i < header.length + 1; i++) {
var c = header[i];
if (state == 0) {
if (c == "\"") {
current.push(temp);
temp = "";
state = -1;
} else {
temp += c;
}
} else if (state == 1) {
if (c == ">") {
current.push(temp);
info.push (current);
current = [];
temp = "";
state = -1;
} else {
temp += c;
}
} else {
if (c == "<"){
state = 1;
} else if (c == "\"") {
state = 0;
}
}
}
alert ("INFO: \n" + info);
For something complete, you should port this to JS: http://cpansearch.perl.org/src/RJBS/Email-Address-1.895/lib/Email/Address.pm
It gives you all the parts you need. The tricky bit is just the set of regexps at the start.

Resources