Using preg_replace in laravel
I have database data like text [mydata] some text [otherdata] some more text
and I want to replace those [mydata]
, [otherdata]
with dynamic info such as category title for instance.
So the result of my data would be like:
text Category Title some text Category Desc some more text
instead of
text [mydata] some text [otherdata] some more text
I think that can be happen by preg_replace
but not quite sure.
code
View::composer('*', function ($view)
$notes = Notes::all();
foreach($notes as $note)
$descrip= $note->description;
$view->with('descrip', $descrip);
);
More
So basically $note->description
content is this data:
text [mydata] some text [otherdata] some more text
I want to replace those elements by data from categories
table.
Any idea?
Update
well i was digging and get code below (useing str_replace
) however it has some issues,
View::composer('*', function ($view)
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD)
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;
//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);
$view->with('myCustom', $myCustom);
);
Issues
- I can only get
[cattitle]
but what about[catmeta] & [cattags]
? $meta_title
will always return first value from categories table (for example if i'm visitingHP
category page it returnACER
as[cattitle]
or any other category pages i visit. It's always ACER
Update 2
I solved the issue 1 of my first update but still issue 2 remained, here is updated code
View::composer('*', function ($view)
//categories
$catC = Category::all();
$catD = ;
foreach($catC as $catD)
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
$a1 = array("[cattitle]","[catmeta]","[catdesc]");
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);
$view->with('myCustom', $myCustom);
);
Current issue:
$meta_title
will always return first value from categories table (for example if i'm visitingHP
category page it returnACER
as
[cattitle]
or any other category pages i visit. It's always ACER
php laravel preg-replace
add a comment |
I have database data like text [mydata] some text [otherdata] some more text
and I want to replace those [mydata]
, [otherdata]
with dynamic info such as category title for instance.
So the result of my data would be like:
text Category Title some text Category Desc some more text
instead of
text [mydata] some text [otherdata] some more text
I think that can be happen by preg_replace
but not quite sure.
code
View::composer('*', function ($view)
$notes = Notes::all();
foreach($notes as $note)
$descrip= $note->description;
$view->with('descrip', $descrip);
);
More
So basically $note->description
content is this data:
text [mydata] some text [otherdata] some more text
I want to replace those elements by data from categories
table.
Any idea?
Update
well i was digging and get code below (useing str_replace
) however it has some issues,
View::composer('*', function ($view)
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD)
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;
//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);
$view->with('myCustom', $myCustom);
);
Issues
- I can only get
[cattitle]
but what about[catmeta] & [cattags]
? $meta_title
will always return first value from categories table (for example if i'm visitingHP
category page it returnACER
as[cattitle]
or any other category pages i visit. It's always ACER
Update 2
I solved the issue 1 of my first update but still issue 2 remained, here is updated code
View::composer('*', function ($view)
//categories
$catC = Category::all();
$catD = ;
foreach($catC as $catD)
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
$a1 = array("[cattitle]","[catmeta]","[catdesc]");
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);
$view->with('myCustom', $myCustom);
);
Current issue:
$meta_title
will always return first value from categories table (for example if i'm visitingHP
category page it returnACER
as
[cattitle]
or any other category pages i visit. It's always ACER
php laravel preg-replace
You don't need regex. You can usestr_replace
.
– user3783243
Nov 16 '18 at 1:29
@user3783243 I've tried that It has some issue i'll update my question.
– mafortis
Nov 16 '18 at 1:37
@user3783243 updated
– mafortis
Nov 16 '18 at 1:39
Looks like there's some variable naming issue. Your$catD
is not updated because it refers to the variable within the foreach scope. Define the outside variable as$catE
and then inside the loop use$catE = [ $cattitle = $catD->title, ... ]
?
– hktang
Nov 16 '18 at 3:34
@hktang regarding to$catE
result is the same as before i still get acer info instead of hp
– mafortis
Nov 16 '18 at 8:48
add a comment |
I have database data like text [mydata] some text [otherdata] some more text
and I want to replace those [mydata]
, [otherdata]
with dynamic info such as category title for instance.
So the result of my data would be like:
text Category Title some text Category Desc some more text
instead of
text [mydata] some text [otherdata] some more text
I think that can be happen by preg_replace
but not quite sure.
code
View::composer('*', function ($view)
$notes = Notes::all();
foreach($notes as $note)
$descrip= $note->description;
$view->with('descrip', $descrip);
);
More
So basically $note->description
content is this data:
text [mydata] some text [otherdata] some more text
I want to replace those elements by data from categories
table.
Any idea?
Update
well i was digging and get code below (useing str_replace
) however it has some issues,
View::composer('*', function ($view)
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD)
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;
//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);
$view->with('myCustom', $myCustom);
);
Issues
- I can only get
[cattitle]
but what about[catmeta] & [cattags]
? $meta_title
will always return first value from categories table (for example if i'm visitingHP
category page it returnACER
as[cattitle]
or any other category pages i visit. It's always ACER
Update 2
I solved the issue 1 of my first update but still issue 2 remained, here is updated code
View::composer('*', function ($view)
//categories
$catC = Category::all();
$catD = ;
foreach($catC as $catD)
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
$a1 = array("[cattitle]","[catmeta]","[catdesc]");
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);
$view->with('myCustom', $myCustom);
);
Current issue:
$meta_title
will always return first value from categories table (for example if i'm visitingHP
category page it returnACER
as
[cattitle]
or any other category pages i visit. It's always ACER
php laravel preg-replace
I have database data like text [mydata] some text [otherdata] some more text
and I want to replace those [mydata]
, [otherdata]
with dynamic info such as category title for instance.
So the result of my data would be like:
text Category Title some text Category Desc some more text
instead of
text [mydata] some text [otherdata] some more text
I think that can be happen by preg_replace
but not quite sure.
code
View::composer('*', function ($view)
$notes = Notes::all();
foreach($notes as $note)
$descrip= $note->description;
$view->with('descrip', $descrip);
);
More
So basically $note->description
content is this data:
text [mydata] some text [otherdata] some more text
I want to replace those elements by data from categories
table.
Any idea?
Update
well i was digging and get code below (useing str_replace
) however it has some issues,
View::composer('*', function ($view)
//getting categories and pull out the values i want to use as `[....]`
$catC = Category::all();
foreach($catC as $catD)
$meta_title = $catD->title;
$meta_desc = $catD->meta_description;
$meta_tags = $catD->meta_tags;
//replace those categories values with element in database `[......]`
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace_array('[cattitle]', [$meta_title, $meta_desc, $meta_tags], $seotemplate->categories_desc_template);
$view->with('myCustom', $myCustom);
);
Issues
- I can only get
[cattitle]
but what about[catmeta] & [cattags]
? $meta_title
will always return first value from categories table (for example if i'm visitingHP
category page it returnACER
as[cattitle]
or any other category pages i visit. It's always ACER
Update 2
I solved the issue 1 of my first update but still issue 2 remained, here is updated code
View::composer('*', function ($view)
//categories
$catC = Category::all();
$catD = ;
foreach($catC as $catD)
$catD = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
$a1 = array("[cattitle]","[catmeta]","[catdesc]");
$seotemplates = SeoTemplate::all();
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catD, $seotemplate->categories_desc_template);
$view->with('myCustom', $myCustom);
);
Current issue:
$meta_title
will always return first value from categories table (for example if i'm visitingHP
category page it returnACER
as
[cattitle]
or any other category pages i visit. It's always ACER
php laravel preg-replace
php laravel preg-replace
edited Nov 16 '18 at 2:48
mafortis
asked Nov 16 '18 at 0:48
mafortismafortis
1,179943
1,179943
You don't need regex. You can usestr_replace
.
– user3783243
Nov 16 '18 at 1:29
@user3783243 I've tried that It has some issue i'll update my question.
– mafortis
Nov 16 '18 at 1:37
@user3783243 updated
– mafortis
Nov 16 '18 at 1:39
Looks like there's some variable naming issue. Your$catD
is not updated because it refers to the variable within the foreach scope. Define the outside variable as$catE
and then inside the loop use$catE = [ $cattitle = $catD->title, ... ]
?
– hktang
Nov 16 '18 at 3:34
@hktang regarding to$catE
result is the same as before i still get acer info instead of hp
– mafortis
Nov 16 '18 at 8:48
add a comment |
You don't need regex. You can usestr_replace
.
– user3783243
Nov 16 '18 at 1:29
@user3783243 I've tried that It has some issue i'll update my question.
– mafortis
Nov 16 '18 at 1:37
@user3783243 updated
– mafortis
Nov 16 '18 at 1:39
Looks like there's some variable naming issue. Your$catD
is not updated because it refers to the variable within the foreach scope. Define the outside variable as$catE
and then inside the loop use$catE = [ $cattitle = $catD->title, ... ]
?
– hktang
Nov 16 '18 at 3:34
@hktang regarding to$catE
result is the same as before i still get acer info instead of hp
– mafortis
Nov 16 '18 at 8:48
You don't need regex. You can use
str_replace
.– user3783243
Nov 16 '18 at 1:29
You don't need regex. You can use
str_replace
.– user3783243
Nov 16 '18 at 1:29
@user3783243 I've tried that It has some issue i'll update my question.
– mafortis
Nov 16 '18 at 1:37
@user3783243 I've tried that It has some issue i'll update my question.
– mafortis
Nov 16 '18 at 1:37
@user3783243 updated
– mafortis
Nov 16 '18 at 1:39
@user3783243 updated
– mafortis
Nov 16 '18 at 1:39
Looks like there's some variable naming issue. Your
$catD
is not updated because it refers to the variable within the foreach scope. Define the outside variable as $catE
and then inside the loop use $catE = [ $cattitle = $catD->title, ... ]
?– hktang
Nov 16 '18 at 3:34
Looks like there's some variable naming issue. Your
$catD
is not updated because it refers to the variable within the foreach scope. Define the outside variable as $catE
and then inside the loop use $catE = [ $cattitle = $catD->title, ... ]
?– hktang
Nov 16 '18 at 3:34
@hktang regarding to
$catE
result is the same as before i still get acer info instead of hp– mafortis
Nov 16 '18 at 8:48
@hktang regarding to
$catE
result is the same as before i still get acer info instead of hp– mafortis
Nov 16 '18 at 8:48
add a comment |
1 Answer
1
active
oldest
votes
I see a couple of issues with your code that I think will help with getting the solution that you are after.
First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:
$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
Second you are also resetting the value of $myCustom with each loop of the for each. Try this:
$myCustom =
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);
This should result in you getting an array of seotemplates with the values replaced but I have not testing this.
regarding to$catE
i'm gettingArray to string conversion
and i think$myCustom
isn't right either should be equal to;
or'';
– mafortis
Nov 16 '18 at 8:47
Yeah sorry you are right. I have updated my code.
– Josh
Nov 16 '18 at 8:50
shouldn't i getcatE
instr_replace
istead ofcatD
?
– mafortis
Nov 16 '18 at 9:08
and oh,$catE
is already array if i make$catE = [...]
will be array in array right? why would i make that?
– mafortis
Nov 16 '18 at 9:09
Yes your right it should be $catE
– Josh
Nov 16 '18 at 9:12
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329892%2fusing-preg-replace-in-laravel%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I see a couple of issues with your code that I think will help with getting the solution that you are after.
First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:
$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
Second you are also resetting the value of $myCustom with each loop of the for each. Try this:
$myCustom =
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);
This should result in you getting an array of seotemplates with the values replaced but I have not testing this.
regarding to$catE
i'm gettingArray to string conversion
and i think$myCustom
isn't right either should be equal to;
or'';
– mafortis
Nov 16 '18 at 8:47
Yeah sorry you are right. I have updated my code.
– Josh
Nov 16 '18 at 8:50
shouldn't i getcatE
instr_replace
istead ofcatD
?
– mafortis
Nov 16 '18 at 9:08
and oh,$catE
is already array if i make$catE = [...]
will be array in array right? why would i make that?
– mafortis
Nov 16 '18 at 9:09
Yes your right it should be $catE
– Josh
Nov 16 '18 at 9:12
|
show 1 more comment
I see a couple of issues with your code that I think will help with getting the solution that you are after.
First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:
$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
Second you are also resetting the value of $myCustom with each loop of the for each. Try this:
$myCustom =
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);
This should result in you getting an array of seotemplates with the values replaced but I have not testing this.
regarding to$catE
i'm gettingArray to string conversion
and i think$myCustom
isn't right either should be equal to;
or'';
– mafortis
Nov 16 '18 at 8:47
Yeah sorry you are right. I have updated my code.
– Josh
Nov 16 '18 at 8:50
shouldn't i getcatE
instr_replace
istead ofcatD
?
– mafortis
Nov 16 '18 at 9:08
and oh,$catE
is already array if i make$catE = [...]
will be array in array right? why would i make that?
– mafortis
Nov 16 '18 at 9:09
Yes your right it should be $catE
– Josh
Nov 16 '18 at 9:12
|
show 1 more comment
I see a couple of issues with your code that I think will help with getting the solution that you are after.
First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:
$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
Second you are also resetting the value of $myCustom with each loop of the for each. Try this:
$myCustom =
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);
This should result in you getting an array of seotemplates with the values replaced but I have not testing this.
I see a couple of issues with your code that I think will help with getting the solution that you are after.
First like mentioned by @hktang said in his comment you are duplicating your variables and then assigning it's value over and over again instead of adding to it. Try this:
$catE = ;
foreach($catC as $catD)
$catE = [
$cattitle = $catD->title,
$catmeta = $catD->meta_tags,
$catdesc = $catD->meta_description
];
Second you are also resetting the value of $myCustom with each loop of the for each. Try this:
$myCustom =
foreach($seotemplates as $seotemplate)
$myCustom = str_replace($a1, $catE, $seotemplate->categories_desc_template);
This should result in you getting an array of seotemplates with the values replaced but I have not testing this.
edited Nov 16 '18 at 9:11
answered Nov 16 '18 at 5:16
JoshJosh
718217
718217
regarding to$catE
i'm gettingArray to string conversion
and i think$myCustom
isn't right either should be equal to;
or'';
– mafortis
Nov 16 '18 at 8:47
Yeah sorry you are right. I have updated my code.
– Josh
Nov 16 '18 at 8:50
shouldn't i getcatE
instr_replace
istead ofcatD
?
– mafortis
Nov 16 '18 at 9:08
and oh,$catE
is already array if i make$catE = [...]
will be array in array right? why would i make that?
– mafortis
Nov 16 '18 at 9:09
Yes your right it should be $catE
– Josh
Nov 16 '18 at 9:12
|
show 1 more comment
regarding to$catE
i'm gettingArray to string conversion
and i think$myCustom
isn't right either should be equal to;
or'';
– mafortis
Nov 16 '18 at 8:47
Yeah sorry you are right. I have updated my code.
– Josh
Nov 16 '18 at 8:50
shouldn't i getcatE
instr_replace
istead ofcatD
?
– mafortis
Nov 16 '18 at 9:08
and oh,$catE
is already array if i make$catE = [...]
will be array in array right? why would i make that?
– mafortis
Nov 16 '18 at 9:09
Yes your right it should be $catE
– Josh
Nov 16 '18 at 9:12
regarding to
$catE
i'm getting Array to string conversion
and i think $myCustom
isn't right either should be equal to ;
or '';
– mafortis
Nov 16 '18 at 8:47
regarding to
$catE
i'm getting Array to string conversion
and i think $myCustom
isn't right either should be equal to ;
or '';
– mafortis
Nov 16 '18 at 8:47
Yeah sorry you are right. I have updated my code.
– Josh
Nov 16 '18 at 8:50
Yeah sorry you are right. I have updated my code.
– Josh
Nov 16 '18 at 8:50
shouldn't i get
catE
in str_replace
istead of catD
?– mafortis
Nov 16 '18 at 9:08
shouldn't i get
catE
in str_replace
istead of catD
?– mafortis
Nov 16 '18 at 9:08
and oh,
$catE
is already array if i make $catE = [...]
will be array in array right? why would i make that?– mafortis
Nov 16 '18 at 9:09
and oh,
$catE
is already array if i make $catE = [...]
will be array in array right? why would i make that?– mafortis
Nov 16 '18 at 9:09
Yes your right it should be $catE
– Josh
Nov 16 '18 at 9:12
Yes your right it should be $catE
– Josh
Nov 16 '18 at 9:12
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329892%2fusing-preg-replace-in-laravel%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You don't need regex. You can use
str_replace
.– user3783243
Nov 16 '18 at 1:29
@user3783243 I've tried that It has some issue i'll update my question.
– mafortis
Nov 16 '18 at 1:37
@user3783243 updated
– mafortis
Nov 16 '18 at 1:39
Looks like there's some variable naming issue. Your
$catD
is not updated because it refers to the variable within the foreach scope. Define the outside variable as$catE
and then inside the loop use$catE = [ $cattitle = $catD->title, ... ]
?– hktang
Nov 16 '18 at 3:34
@hktang regarding to
$catE
result is the same as before i still get acer info instead of hp– mafortis
Nov 16 '18 at 8:48